0

I've created a batch script to do some basic file management for me. It's passed in either a directory or file path, along with a category. The program will then Copy the file to another directory of my choosing.

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.

Putting in some echo statements shows it erroring right at the top of the IF block without every evaluating any part of it. Outputting each parameter results in this:

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.

Doing something like: script.bat "C:/Folder (Stuff) Test/Folder2" Music is a little better but still has problems:

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.

I've tried lots of combinations and that problems are seem to be caused by having the parenthesis in the parameter variable. If it doesn't break the IF block it just breaks something else elsewhere. How would I go about fixing it? I can't modify the file path, and still have to make a copy letter for letter of that directory with a robocopy command further down the script.

  • When doing your conditional comparisons, you really should be Double quoting: If "%~1" == "Value" (Command) – T3RR0R May 23 at 14:25
0

Ok, I think I figured it out just by throwing lots of quotes everywhere:

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 
)

There were other errors throughout the rest of the program that were fixed just by putting quotes everywhere. Such as:

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

This was just one of times where I wasn't really sure what I was doing but it ended up working.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.