批处理脚本中文件路径参数内的转义括号

批处理脚本中文件路径参数内的转义括号

我创建了一个批处理脚本来为我做一些基本的文件管理。它传入一个目录或文件路径以及一个类别。然后程序会将文件复制到我选择的另一个目录中。

REM First parameter is file or directory path, second is category

SET singleLogFile=D:\Logs\CopyLog.txt
SET masterLogFile=D:\Logs\MasterCopyLog.txt

SET moviePath=D:\Videos\Movies\
SET TVPath=D:\Videos\TV\
SET musicPath=D:\Music\

if %2 EQU Movies (
    echo This is a Movie, I'll put it in the movie folder: %moviePath%
    echo This is a Movie, I'll put it in the movie folder: %moviePath% >> %singleLogFile%
    SET copyPath=%moviePath%%~n1
) else if %2 EQU TV (
    echo This is a TV Show, I'll put it in the TV folder: %TVPath%
    echo This is a TV Show, I'll put it in the TV folder: %TVPath% >> %singleLogFile%
    SET copyPath=%TVPath%%~n1
) else if %2 EQU Music (
    echo This is Music, I'll put it in the Music folder: %musicPath%
    echo This is Music, I'll put it in the Music folder: %musicPath% >> %singleLogFile%
    SET copyPath=%musicPath%%~n1
) else (
    echo An item with an invalid category came through here:
    echo PATH: %1 
    echo Category %2

    echo An item with an invalid category came through here: >> %singleLogFile%
    echo PATH: %1 >> %singleLogFile%
    echo Category %2 >> %singleLogFile%
    GOTO:exitScript 
)

Other logic and stuff down here.
Check if it's a file or folder
robocopy it to %copyPath%
exit the script

传入类似这样的内容script.bat "C:/Folder/Folder2" Music就可以了。但是,一旦我到达带有括号的文件夹或文件,一旦到达该 IF 块,一切都会中断。

例如:script.bat "C:/Folder (ABC)/Folder2 (MORE) (OTHER)" Music将导致此错误:

(OTHER) was unexpected at this time.

放入一些 echo 语句表明它在 IF 块的顶​​部出错,而无需评估其任何部分。输出每个参数的结果如下:

script.bat
echo PATH: %1
echo Category %2

if %2 EQU Movies (
...

results:
PATH: "C:/Folder (ABC)/Folder2 (MORE) (OTHER)"
Category: Music
(OTHER) was unexpected at this time.

这样做script.bat "C:/Folder (Stuff) Test/Folder2" Music会好一点,但仍然存在问题:

PATH: "C:/Folder (STUFF) Test/Folder2"
Category: Music
This is Music, I'll put it in the Music folder: D:\Music\
I'm going to go put that file right here: D:\Music\Folder2
Test\Folder2 was unexpected at this time.

我尝试了很多组合,问题似乎是由参数变量中的括号引起的。如果它没有破坏 IF 块,它只会破坏其他地方。我该如何修复它?我无法修改文件路径,仍然必须使用脚本下方的 robocopy 命令逐个复制该目录的字母。

答案1

好的,我想我只是通过在各处引用大量引号就明白了:

if "%2" EQU "Movies" (
    echo This is a Movie, I'll put it in the movie folder: %moviePath%
    echo This is a Movie, I'll put it in the movie folder: %moviePath% >> %singleLogFile%
    SET "copyPath=%moviePath%%~n1"
) else if "%2" EQU "TV" (
    echo This is a TV Show, I'll put it in the TV folder: %TVPath%
    echo This is a TV Show, I'll put it in the TV folder: %TVPath% >> %singleLogFile%
    SET "copyPath=%TVPath%%~n1"
) else if "%2" EQU "Music" (
    echo This is Music, I'll put it in the Music folder: %musicPath%
    echo This is Music, I'll put it in the Music folder: %musicPath% >> %singleLogFile%
    SET "copyPath=%musicPath%%~n1"
) else (
    echo An item with an invalid category came through here:
    echo PATH: %1 
    echo Category %2

    echo An item with an invalid category came through here: >> %singleLogFile%
    echo PATH: %1 >> %singleLogFile%
    echo Category %2 >> %singleLogFile%
    GOTO:exitScript 
)

程序其余部分还存在其他错误,只需在所有地方加上引号即可修复。例如:

robocopy "%~dp1" "%copyPath%" "%~nx1" /A-:SH /R:15 /NP /ETA /LOG:%singleLogFile% /TEE
attrib -h -s "%copyPath%"

这只是其中一次我不太确定自己在做什么但最终还是成功了。

相关内容