【cmd】IF ELSE 複製(copy)檔案問題

c-xuan發表於2017-11-28

cmd中複製檔案COPY命令一般都不會有問題,但是如果把COPY放在IF ELSE中可能導致批處理檔案無法執行。

測試場景

資料夾結構如下:

test
|—folder1
|—|—a(b).txt
|—folder2

選擇是否從folder1資料夾複製a(b).txt檔案到folder2資料夾。

測試1

不進行選擇互動,直接複製,指令碼如下:

@echo off & setlocal EnableDelayedExpansion

set currentDir=!cd!

DEL /Q !currentDir!\folder2\*.* 

copy !currentDir!\folder1\a(b).txt !currentDir!\folder2

儲存為test.bat檔案後執行結果:

已複製         1 個檔案。
請按任意鍵繼續. . .

copy複製語句似乎沒有問題。

測試2

修改以上指令碼,新增選擇互動:

@echo off & setlocal EnableDelayedExpansion

set /p yesno=是否複製(0:否,1:是): 

set currentDir=!cd!

DEL /Q !currentDir!\folder2\*.*

if !yesno! equ 1 ( 
    copy !currentDir!\folder1\a(b).txt !currentDir!\folder2
)else (
    echo 沒複製
)

pause

儲存為test.bat檔案後執行,發現一閃而過,看不到什麼報錯。

解決

經過多次測試,發現將copy中的路徑用雙引號(“”)包裹起來就可以了。

@echo off & setlocal EnableDelayedExpansion

set /p yesno=是否複製(0:否,1:是): 

set currentDir=!cd!

DEL /Q !currentDir!\folder2\*.*

if !yesno! equ 1 ( 
    copy "!currentDir!\folder1\a(b).txt" !currentDir!\folder2
)else (
    echo 沒複製
)

pause

由此可見,應該是路徑中某些符號沒有轉義導致的,目測是檔名中的(),修改指令碼,用^()轉義:

@echo off & setlocal EnableDelayedExpansion

set /p yesno=是否複製(0:否,1:是): 

set currentDir=!cd!

DEL /Q !currentDir!\folder2\*.*

if !yesno! equ 1 ( 
    copy !currentDir!\folder1\a^(b^).txt !currentDir!\folder2
)else (
    echo 沒複製
)

pause

執行結果:

是否複製(0:否,1:是): 1
已複製         1 個檔案。
請按任意鍵繼續. . .

總結

雖然測試1中直接執行復制沒問題,但是,將同樣的語句放入IF ELSE中居然無法執行,還很難定位問題在哪裡,所以解決方法是最好把路徑放在雙引號(“”)裡面,就不用擔心這個問題了,如果不這樣就在IF ELSE中的路徑把特殊字元轉義。附上CMD中特殊字元轉義說明。

Character to be escaped Escape Sequence Remark
% %% May not always be required in doublequoted strings, just try
^ ^^ May not always be required in doublequoted strings, but it won’t hurt
& ^& May not always be required in doublequoted strings, but it won’t hurt
< ^< May not always be required in doublequoted strings, but it won’t hurt
> ^> May not always be required in doublequoted strings, but it won’t hurt
^’ Required only in the FOR /F “subject” (i.e. between the parenthesis), unless backqis used
` ^` Required only in the FOR /F “subject” (i.e. between the parenthesis), if backq is used
, ^, Required only in the FOR /F “subject” (i.e. between the parenthesis), even in doublequoted strings
; ^; Required only in the FOR /F “subject” (i.e. between the parenthesis), even in doublequoted strings
= ^= Required only in the FOR /F “subject” (i.e. between the parenthesis), even in doublequoted strings
( ^( Required only in the FOR /F “subject” (i.e. between the parenthesis), even in doublequoted strings
) ^) Required only in the FOR /F “subject” (i.e. between the parenthesis), even in doublequoted strings
! ^^! Required only when delayed variable expansion is active
“” Required only inside the search pattern of FIND
\ \\ Required only inside the regex pattern of FINDSTR
[ \[ Required only inside the regex pattern of FINDSTR
] \] Required only inside the regex pattern of FINDSTR
\ \\ Required only inside the regex pattern of FINDSTR
. \. Required only inside the regex pattern of FINDSTR
* \* Required only inside the regex pattern of FINDSTR
? \? Required only inside the regex pattern of FINDSTR

參考:http://www.robvanderwoude.com/escapechars.php

將表格快速轉換為HTML,Markdown格式:http://www.tablesgenerator.com/

更多
c-xuan.com/2017/11/28/20171128001

相關文章