windows 批处理脚本 - 如果存在复制到 %localappdata% 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6570313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:33:11  来源:igfitidea点击:

Batch Script - IF EXIST copy to %localappdata% error

windowsbatch-file

提问by Sierry

I seem to be stuck with a batch script and would like some help.

我似乎被批处理脚本困住了,需要一些帮助。

Basically I need to check if a file exists in a folder in the %localappdata%and if it does then overwrite the file and if not place inside a different location so at the moment it reads like this:

基本上我需要检查一个文件是否存在于文件夹中%localappdata%,如果它覆盖文件,如果没有放在不同的位置,所以现在它读起来像这样:

IF EXIST "%localappdata%\foldername\filename" COPY /Y "filename" "location" ELSE COPY "filename" "location2" 

But when this runs I'm receiving an error of The syntax of the command is incorrect.This seems to be down to the %localappdata%variable being used.

但是当它运行时,我收到一个错误The syntax of the command is incorrect.这似乎归结为%localappdata%正在使用的变量。

Thank you in advance for any help on this.

预先感谢您对此的任何帮助。

回答by Jon

You need to put the two commands for the IFbranches in parens:

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

IF EXIST "%localappdata%\foldername\filename" (COPY /Y "filename" "location") ELSE (COPY "filename" "location2")

The reason for this is that the shell needs to be able to tell that if the file does exist, the command you want to run is just this:

这样做的原因是 shell 需要能够判断文件是否存在,您要运行的命令是这样的:

COPY /Y "filename" "location"

and notall of this:

不是所有这些:

COPY /Y "filename" "location" ELSE COPY "filename" "location2"

If you think about it, all those ELSE COPYstuff may very well be legitimate parameters for the first COPY-- the shell has no way to know unless you help.

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