批处理脚本 - IF EXIST复制到%localappdata%错误

Sie*_*rry 6 windows batch-file

我似乎陷入了批处理脚本,并希望得到一些帮助.

基本上我需要检查文件中是否存在文件%localappdata%,如果它存在,则覆盖文件,如果没有放在不同的位置,所以此时它的内容如下:

IF EXIST "%localappdata%\foldername\filename" COPY /Y "filename" "location" ELSE COPY "filename" "location2" 
Run Code Online (Sandbox Code Playgroud)

但是当这个运行时,我收到一个错误.The syntax of the command is incorrect. 这似乎是%localappdata%由于使用的变量.

提前感谢您对此的任何帮助.

Jon*_*Jon 7

您需要将两个命令IF放在parens中的分支中:

IF EXIST "%localappdata%\foldername\filename" (COPY /Y "filename" "location") ELSE (COPY "filename" "location2")
Run Code Online (Sandbox Code Playgroud)

原因是shell需要能够告诉您文件是否存在,您要运行的命令就是:

COPY /Y "filename" "location"
Run Code Online (Sandbox Code Playgroud)

不是所有这些:

COPY /Y "filename" "location" ELSE COPY "filename" "location2"
Run Code Online (Sandbox Code Playgroud)

如果你考虑一下,所有这些ELSE COPY东西很可能是第一个合法的参数COPY- 除非你帮忙,否则shell无法知道.