2011-07-04 50 views
4

我似乎被卡在一個批處理腳本,並希望得到一些幫助。批處理腳本 - 如果存在複製到%localappdata%錯誤

基本上我需要檢查文件是否在%localappdata%文件夾中存在,如果它不然後覆蓋該文件,如果沒有放置不同的位置內,從而在目前它讀起來就像這樣:

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

但是,當這個運行我收到一個錯誤The syntax of the command is incorrect.這似乎是下降到%localappdata%變量正在使用。

非常感謝您對此提供任何幫助。

回答

5

你需要把兩個命令爲IF分支機構的括號:

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

這樣做的原因是,外殼需要能夠告訴如果文件不存在,你想要的命令運行僅僅是這樣的:

COPY /Y "filename" "location" 

這一切:

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

如果你仔細想想,那麼所有這些ELSE COPY東西都可能是第一個合法的參數COPY - 除非你有幫助,否則shell無法知道。

+0

Brilliant Jon。 我知道這會是直接的!這是完全合理的。非常感謝您的回答。 – Sierry