在批处理脚本中的参数内转义括号

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

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

Passing in something like script.bat "C:/Folder/Folder2" Music works just fine. But as soon as I get to a folder or file with parenthesis in it everything breaks as soon as I get to that IF block.

For example: script.bat "C:/Folder (ABC)/Folder2 (MORE) (OTHER)" Music will result in this error:

(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.

显然,该文件路径中的空格和括号会破坏IF块。我将如何解决它?我无法修改文件路径,但仍然必须使用脚本后面的robocopy命令为该目录的字母制作一个复制字母。

评论