Copy Files from Text List with spaces

Hercules - Feb 26, 2009 at 07:33 AM
 the SASS Man - Sep 9, 2010 at 07:48 PM

I am trying to create a batch file to copy files from a list of locations in a .txt file to a new location.

The code below gives me an error, because the files have spaces in the names.

@echo off
echo -- > C:\TestingTMP.txt
xcopy /y C:\Testing.m3u C:\TestingTMP.txt
find "H:\Music" C:\TestingTMP.txt >> C:\Testing.txt
for /f %%a in (C:\Testing.txt) do (
xcopy /y %%a H:\Music\Testing

H:\Music\Kutless\Kutless - Sea Of Faces.mp3
H:\Music\Tree 63\The Life And Times Of Absolute - Tree 63 - How Did I Sleep.mp3

Using a third party program (because all the forums indicate this is what it will take to do text find and replace), I have changed the list generated in "Testing.txt" and put inverted comma's around the files with spaces.

H:\"Music"\"Kutless"\"Kutless - Sea Of Faces".mp3
H:\"Music"\"Tree 63"\"The Life And Times Of Absolute - Tree 63 - How Did I Sleep".mp3

Now the code
for /f %%a in (C:\Testing.txt) do (
xcopy /y %%a H:\Music\Testing
gives me a "Parse Error"! And it still only looks at the title before the first space!
c:\>(xcopy /y H:\"Music"\"Kutless"\"Kutless - Sea H:\Music\Testing
Parse Error

Can anyone please shed some light on this supposed to be simple function and how to solve the problem?

4 replies

athenazob Posts 5 Registration date Tuesday September 16, 2008 Status Member Last seen February 27, 2009 8
Feb 27, 2009 at 01:41 AM
in fact you should use your original list and modify the "For /F" statement as written downwards:

for /F "delims=" %%a in (C:\Testing.txt) do (
xcopy /Y "%%a" H:\Music\Testing

hope it helps you.
All you should really have to do is, put quotes around the filename itself in the XCopy so:

xcopy /y "%%a" H:\Music\Testing

That way, whether the filenames have spaces or not, Windows will expand "%%a" to "Your Filename.ext", and the copy should work perfectly.

Oh, one more order to get past the "Tokenizing" process of the For command, you MUST use the following option:

/F "delimeter="

This tells the system that the ENTIRE LINE from your source file is a single "token" to be passed to the command(s) inside the parenthesis. For's default behavior is to basically treat every space and tab character it finds as the equivalent as an EOL character. Unless the user defines some special way of parcing the line.

you can get more information, including info specific to your operating system, by typing "help for" at the command line.

Bleach! My fingers are faster than my brain ATm....

That option is:

/F "delims="

Only Microsoft could make a less intuitive operating me.
You might try using PullaFile. Here is the link: