批处理文件嵌套的IF语句帮助 - CMD批处理

时间:2022-03-18 02:22:32

I'm currently in the process of writing a simple batch file. I'm not new to programming, but by no means an expert. This is the first time I've used batch files.

我目前正在编写一个简单的批处理文件。我不是编程新手,但绝不是专家。这是我第一次使用批处理文件。

Basically I want a file from one directory to be moved to another, and, if there is already a file with the same name in the target directory, it will rename it as a backup. This way it will always keep a backup in case this script is accidentally run.

基本上我希望将一个目录中的文件移动到另一个目录,如果目标目录中已存在同名文件,则会将其重命名为备份。这样,它将始终保留备份,以防意外运行此脚本。

After hours of troubleshooting my code, I still don't know why It doesn't work. I suspect it's something to do with nested if statements. An If statement inside an if statement (for a lack of a better explanation).

经过几个小时的代码疑难解答,我仍然不知道为什么它不起作用。我怀疑它与嵌套的if语句有关。 if语句中的If语句(缺少更好的解释)。

I would appreciate the help. Thanks

我很感激帮助。谢谢

  @echo off

REM Variables
rem dir1 is the source directory
set dir1=C:\Users\%username%\Desktop\Dir1\
rem dir2 is the target directory
set dir2=C:\Users\%username%\Desktop\Dir2\
rem file is the name of the source file and desired name of the target file
set file=file.txt
rem pre1 is the name of the copy of the old target file before it is overwritten
set pre1=push backup
rem pre2 is the name of prefix for a temporary file created in the target directory
set pre2=temp

REM if source file exist
if exist %dir1%%file% (
    rem if there is a file named the same in target directory
    if exist %dir2%%file% (
        rem if there is allready a backup file
        if exist %dir2%%pre1%%file% (
            rem rename the backup file as a temp file
            rename %dir2%%pre1%%file% %dir2%%pre2%%file%
            rem rename the old file as backup file
            rename %dir2%%file% %dir2%%pre1%%file%

            rem move source file to target directory
            move %dir1%%file% %dir2%

            rem if rename failed
            if not exist %dir2%%file% (
                rem rename the backup to the normal file
                rename %dir2%%pre1%%file% %file%
                rem rename the temp file to the backup file
                rename %dir2%%pre2%%file% %pre1%%file%
                rem echo error
                echo rename error
                pause
            rem Delete temp file
            ) else ( del %dir2%%pre2%%file% )
        )
    ) ELSE ( move %dir1%%file% %dir2% )
) ELSE ( echo %file% does not exist in directory "%dir1%" )
pause

Apologies for my sloppy commenting.

为我的草率评论道歉。

2 个解决方案

#1


1  

Wrap all your file references in double quotes. Spaces in usernames or file names will break most batch scripts otherwise.

用双引号括起所有文件引用。用户名或文件名中的空格将破坏大多数批处理脚本。

E.g. your pre1 variable is equal to push backup, which contains a space.

例如。你的pre1变量等于push backup,它包含一个空格。

So your 3rd exist test:

所以你的第三个存在测试:

if exist %dir2%%pre1%%file% ( ... )

Will translate to (evaluating the other variables and assuming username is jack):

将转换为(评估其他变量并假设用户名为jack):

if exist C:\Users\jack\Desktop\Dir2\push backupfile.txt ( ... )

This will check for a file named:

这将检查名为的文件:

push

in folder:

C:\Users\jack\Desktop\Dir2\

And if that file exists, then the command that should be executed is:

如果该文件存在,那么应该执行的命令是:

backupfile.txt ( ... )

backupfile.txt(...)

By wrapping file references in double quotes:

通过用双引号包装文件引用:

if exist "%dir2%%pre1%%file%" ( ... )

You get the desired check:

你得到了理想的支票:

if exist "C:\Users\jack\Desktop\Dir2\push backupfile.txt" ( ... )

#2


-2  

You are missing an else for the first IF and a right paean for the true on the first IF.

你错过了第一个IF的else和第一个IF的true的右侧paan。

Should have ') else ()' to complete syntax.

应该有')else()'来完成语法。

For some reason they show now.

由于某种原因,他们现在显示。

Add an else () for the one you don't have.

为你没有的那个添加else()。

#1


1  

Wrap all your file references in double quotes. Spaces in usernames or file names will break most batch scripts otherwise.

用双引号括起所有文件引用。用户名或文件名中的空格将破坏大多数批处理脚本。

E.g. your pre1 variable is equal to push backup, which contains a space.

例如。你的pre1变量等于push backup,它包含一个空格。

So your 3rd exist test:

所以你的第三个存在测试:

if exist %dir2%%pre1%%file% ( ... )

Will translate to (evaluating the other variables and assuming username is jack):

将转换为(评估其他变量并假设用户名为jack):

if exist C:\Users\jack\Desktop\Dir2\push backupfile.txt ( ... )

This will check for a file named:

这将检查名为的文件:

push

in folder:

C:\Users\jack\Desktop\Dir2\

And if that file exists, then the command that should be executed is:

如果该文件存在,那么应该执行的命令是:

backupfile.txt ( ... )

backupfile.txt(...)

By wrapping file references in double quotes:

通过用双引号包装文件引用:

if exist "%dir2%%pre1%%file%" ( ... )

You get the desired check:

你得到了理想的支票:

if exist "C:\Users\jack\Desktop\Dir2\push backupfile.txt" ( ... )

#2


-2  

You are missing an else for the first IF and a right paean for the true on the first IF.

你错过了第一个IF的else和第一个IF的true的右侧paan。

Should have ') else ()' to complete syntax.

应该有')else()'来完成语法。

For some reason they show now.

由于某种原因,他们现在显示。

Add an else () for the one you don't have.

为你没有的那个添加else()。