AndyG
AndyG

Reputation: 47

Reading input from a file doesn't work for filenames containing spaces

I have a case where I have to move over 15,000 files. There are many duplicates, so I parsed them by MD5 and now I have a list of file names in an input file (files.txt). I want to read from it, then copy the listed files to a new directory. I pulled an old batch that someone had written as a two-part simple script and modified it.

It works for files without spaces. How can I get this to cover all file names? Also, can't I put all of this into one file?

Cstart.bat:

for /f %%x in (files.txt) do call copyfiles.bat

copyfiles.bat:

set filename=%1
copy "C:\temp\%filename%" "C:\temp\pruned files\"

Upvotes: 1

Views: 159

Answers (2)

Endoro
Endoro

Reputation: 37569

for /f "usebackqdelims=" %%x in ("my file list.txt") do copy "C:\temp\%%~x" "C:\temp\pruned files"

Upvotes: 0

Ken White
Ken White

Reputation: 125679

Your current code doesn't even pass the filename to copyfiles.bat, so it's not working with or without spaces. (If you need to confirm that, add echo %1 %filename & pause before the copy line in copyfiles.bat and run cstart.bat.)

With that being said, you can do it all in one file easily:

for /f %%x "tokens=1 delims=*" in (files.txt) do copy "C:\Temp\%%x" "C:\Temp\pruned files\%%x"

To make sure it works, just replace the copy in the line above with echo and run it from a command prompt.

I tested this with a text file named test.txt that contained the following:

One.txt
Two.txt
Three.txt
And Four.txt

with a batch file named testcopy.bat containing this:

@echo off
for /f "tokens=1 delims=*" %%x in (test.txt) do echo "C:\Temp\%%x" "C:\Temp\test this\%%x"

The above test showed this output:

e:\TempFiles>testcopy
"C:\Temp\One.txt" "C:\Temp\test this\One.txt"
"C:\Temp\Two.txt" "C:\Temp\test this\Two.txt"
"C:\Temp\Three.txt" "C:\Temp\test this\Three.txt"
"C:\Temp\And Four.txt" "C:\Temp\test this\And Four.txt"

Upvotes: 1

Related Questions