Reputation: 1
I am attempting to create a batch file to backup the directories in my \iTunes\Movies\ folder to an external drive if they do not already exist on the external drive:
@echo off
cls
SetLocal EnableDelayedExpansion
for /f "delims=" %%F in ('dir /ad /b') do (
if not exist g:\movies\%%F (
echo Copying %%F to G:\Movies\%%F
rem md G:\Movies\%%F
rem copy M:\iTunes\Movies\%%F\*.* G:\Movies\%%F
)
)
and here is the output:
Code:
Copying The Princess Bride to G:\Movies\The Princess Bride
Copying Green to G:\Movies\Green
Copying Red to G:\Movies\Red
Copying Princess to G:\Movies\Princess
Copying Bride to G:\Movies\Bride
Copying The Green Mile to G:\Movies\The Green Mile
Copying The Shawshank Redemption to G:\Movies\The Shawshank Redemption
Copying Grape to G:\Movies\Grape
Copying Roof to G:\Movies\Roof
Copying On to G:\Movies\On
Copying Fiddler On the Roof to G:\Movies\Fiddler On the Roof
Copying Brief to G:\Movies\Brief
Copying Number to G:\Movies\Number
Copying to to G:\Movies\to
Copying the to G:\Movies\the
Copying Gilbert Grape to G:\Movies\Gilbert Grape
Copying Torino to G:\Movies\Torino
Copying Sherlock Holmes to G:\Movies\Sherlock Holmes
Copying Gran Torino to G:\Movies\Gran Torino
Copying I Am Number Four to G:\Movies\I Am Number Four
Copying Too Big to Fail to G:\Movies\Too Big to Fail
Copying Pelican to G:\Movies\Pelican
Copying The Shunning to G:\Movies\The Shunning
Copying Inside Job to G:\Movies\Inside Job
Copying Shawshank to G:\Movies\Shawshank
Copying Holmes to G:\Movies\Holmes
Copying Four to G:\Movies\Four
Copying Am to G:\Movies\Am
Copying Big to G:\Movies\Big
Copying Job to G:\Movies\Job
Copying Mile to G:\Movies\Mile
Copying Fail to G:\Movies\Fail
Copying The Pelican Brief to G:\Movies\The Pelican Brief
Copying Shunning to G:\Movies\Shunning
Copying Redemption to G:\Movies\Redemption
Copying Labyrinth to G:\Movies\Labyrinth
Copying The Hunger Games to G:\Movies\The Hunger Games
M:\iTunes\Movies>
it is not only correctly using the full directory name of say, "The Green Mile", but also incorrectly using "The" and "Green" and "Mile" for each directory that exists in the iTunes\Movies folder but does not exist in the External drive.
I have tried it with and without EnableDelayedExpansion and tried various types of variable assignments to no avail.
I just don't get it, I have "delims=" programmed and I have tried it with double quotes around the %%F so why is it breaking up the expression in to individual elements?
Upvotes: 0
Views: 68
Reputation: 11367
Here is my rendition. Since there are several enhancements that will benefit your script. The only thing that it needed to fix it though were some quotations around the exist, md, and copy paths.
~nx
options to remove surrounding quotes and use only the name of the folder in the event that the command line parser decides to change how they return values. (Future Proof)\
to the end of the exist and copy path to specify that it is a directory and not to match any files that may have the same name.copy
after the md
with a &&
meaning that the copy will only happen if the directory is successfully created.Code:
@echo off
cls
SetLocal EnableDelayedExpansion
for /d %%D in (*) do if not exist "G:\Movies\%%~nxD\" (
echo Copying %%~fD to G:\Movies\%%~nxD
rem md "G:\Movies\%%~nxD" && copy "%%~fD\*.*" "G:\Movies\%%~nxD\"
)
I would also recommend looking into the xcopy
and robocopy
commands if you need more options.
Upvotes: 1
Reputation: 12985
You need the quotes around the names in the commands themselves and you need the tokens=* part (as explained here "What delimiter to use in FOR loop for reading lines?"
@echo off
cls
SetLocal EnableDelayedExpansion
for /f "tokens=*" %%F in ('dir /ad /b') do (
if not exist "g:\movies\%%F" (
echo Copying "%%F" to "G:\Movies\%%F"
rem md "G:\Movies\%%F"
rem copy "M:\iTunes\Movies\%%F\*.*" "G:\Movies\%%F"
)
)
This need for quotes is true whenever you have a parameter in a Windows command that is being built from a batch variable that might contain a space (as file names do)
Upvotes: 1