# Solved: path is expanding to individual elements?

Discussion in 'DOS/Other' started by ttx336, Dec 31, 2012.

Not open for further replies.

Joined:
Nov 7, 2012
Messages:
49
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:

Code:
 @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 so why is it breaking up the expression in to individual elements?

Joined:
Apr 4, 2003
Messages:
19,786
Any time you have spaces in your path or file names you need to put them in quotes!

You have no variables that would even use Delayed Expansion so not sure why you would even think that would solve the issue.

Joined:
Nov 7, 2012
Messages:
49
I had Delayed Expansion on because I tried usebackq...

I tried putting in quotes but it didn't seem to help, I tried it again upon your suggestion but I guess I am not getting them in the right places... I still get the same result. Where should I place them? Single or Double Quotes?

I've got it pared down now to only the for loop for testing, no quotes, and I see that Delayed Expansion is NOT necessary for usebackq, I thought it was...

Code:
@echo off
cls

for /f "usebackq delims=" %%f in (dir /ad /b) do echo Filename: %%f
...still the same result, of course.

Joined:
Apr 4, 2003
Messages:
19,786
You don't need USEBACKQ either.

Double quotes around your file names and paths.

Joined:
Nov 7, 2012
Messages:
49
Quotes around %%f after the echo command just puts the quotes out with the correct and incorrect filename

Code:
@echo off
cls

for /f "delims=" %%f in ('dir /ad /b') do echo Filename: "%%f"

Quotes around %%f after "delims=" will not run...

Code:
@echo off
cls

for /f "delims=" "%%f" in ('dir /ad /b') do echo Filename: %%f

the code above produces this error:

Code:
"%f" was unexpected at this time.
M:\iTunes\Movies>

Quotes around the directory command has the same result as using only single quotes...

Code:
@echo off
cls

for /f "delims=" %%f in (' "dir /ad /b" ') do echo Filename: %%f

I don't know where else I could put them that might effect the proper result, any ideas?

Joined:
Apr 4, 2003
Messages:
19,786
Code:
for /f "delims=" %%f in ('dir /ad /b') do echo Filename: "%%f"
That is the correct way to do it.

Just for the sake of argument open up a cmd prompt where your movies are and type: dir /ad /b
Post that output here.

You really don't need to use the DIR command inside a FOR /F loop. You can just use FOR /D.
Code:
FOR /D %%G IN (*) do echo %%G

Joined:
Nov 7, 2012
Messages:
49
I was being an idiot - again... as you apparently suspected.

In one of my earlier, unsuccessful, attempts at this, I had inadvertently created a batch command that created those "One Word" directories in the SOURCE directory. I had pretty much given up on this for now and was going to manually copy the directories in Windows... it was then that I saw the erroneous directories in the SOURCE. I came back here to report my mistake and say that I think I have it going now, and I saw your request for a directory output from the SOURCE directory. I rather hoped I could delete my post before anyone saw it... * embarrassed grin* Up to then I had only been looking at the DESTINATION directory as I am very familiar with the source directory contents, or so I thought.

But, thanks for your patience, and thanks very much for once again giving me a tip on tightening up the code with the

Code:
FOR /D %%G IN (*) do echo %%G
instructions, I really appreciate things like this. I am watching the batch file run right now, it seems to be doing well.

-Gary

Joined:
Nov 7, 2012
Messages:
49
Code:
@echo off
cls

for /d %%X in (*) do (
if not exist "G:\Movies\%%X" 	(
echo Directory: "G:\Movies\%%X" is being created
md "G:\Movies\%%X"
echo Filename: "%%X" is being copied
copy "M:\iTunes\Movies\%%X\*.*" "G:\Movies\%%X\"
)
)



As Seen On