Move files from one Folder to Another based on Filename list (CSV file)

I need a utility to "move" files from one folder and it's sub folders to another folder (recreating the sub folders as needed), based on an input file containing the list of file names to move.  This can be a simple text file with one file name per line, or an excel file containing the list of names.  The point is to remove the list of files from folder A and put them in folder B, replicating the subdirectories in folder B as needed.
Steve MeyerSystem Analyst and DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steve MeyerSystem Analyst and DeveloperAuthor Commented:
Additional information.   I have extracted a list of all attached files in an outlook PST file based on certain search criteria.   These files originated from Folder A and were attached to various emails over a long period of time.  I need to move the list of attached files from folder A to folder B, so that folder A no longer contains any files that are email attachments, and folder B contains the email attachments.  I can create Folder B by simply extracting the attachments from the PST rather than a list of attachments, but I must remove these files from Folder A.
0
Lionel MMSmall Business IT ConsultantCommented:
You can use robocopy for this. This assumes only one filename per line
for /F "skip=1 tokens=1 delims=," %%i in (C:\FileList.csv) do robocopy c:\folder-a c:\folder-b %%1 /r:1/w:1 /move /s
/mov will move files from folder a to folder b but not directories/folders
/move will move files and folders/directories
/s will move subfolders too
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Steve MeyerSystem Analyst and DeveloperAuthor Commented:
Having trouble with your robocopy command.

Attached is my input file.  

I created folder-a which includes several files and a subfolder, like this:

testa.txt
testb.txt
testc.txt
testc 123.txt
testd.txt
teste.txt
subfolder
     testa.txt
     testb.txt
     textc.txt
     textc 123.txt

My input filelist is a text file (filelist.txt) that contains the following lines
testb.txt
testd.txt
testc 123.txt

I put this command in a batch file  (/r:1/w:1 needs a space between parameters, and I renamed filelist.csv to filelist.txt):

for /F "skip=1 tokens=1 delims=," %%i in (C:\FileList.txt) do robocopy c:\folder-a c:\folder-b %%1 /r:1 /w:1 /move /s

Tried skip=0 (robocopy didn't like that), and I tried delims= " and delims="

The command creates folder-b, but it is empty.

folder-b should look like this:

testb.txt
testc 123.txt
testd.txt
subfolder
     testb.txt
     testc 123.txt

And folder a should look like this:
testa.txt
testc.txt
teste.txt
subfolder
     testa.txt
     textc.txt
0
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Lionel MMSmall Business IT ConsultantCommented:
can you give me a copy of your batch file?  
and is the FileList.txt a file with no coding
try running just
robocopy c:\folder-a c:\folder-b testb.txt /r:1 /w:1 /move /s
and tell me what happens
0
Steve MeyerSystem Analyst and DeveloperAuthor Commented:
Running
robocopy c:\folder-a c:\folder-b testb.txt /r:1 /w:1 /move /s
works fine, and because all files are moved, folder-a is subsequently deleted, which is okay.

My input file and batch file are attached.  Site doesn't allow BAT files to be uploaded, so rename Extract.txt to Extract.bat
Extract.txt
Filelist.txt
0
Steve MeyerSystem Analyst and DeveloperAuthor Commented:
This works, except only on filenames in FileList.txt that don't have spaces in them.  If a filename contains any spaces, than each word is treated like a separate filename.

for /F "tokens=*" %%i in (FileList.txt) do robocopy folder-a folder-b %%i /r:1 /w:1 /move /s

I have a file named "testc 123.txt".   This is treated like two files, testc and 123.txt.   My actual file list contains thousands of filenames that each contain 1-20 spaces in the filename.
0
Steve MeyerSystem Analyst and DeveloperAuthor Commented:
I see an option called usebackq, but don't know how to apply it.
0
Lionel MMSmall Business IT ConsultantCommented:
This may help you because if you have names with spaces these must be put in quotes

http://www.computerhope.com/forhlp.htm
usebackq
specifies that the new semantics are in force, where a back quoted string is executed as a command and a single quoted string is a literal string command and allows the use of double quotes to quote file names in filenameset.
Some examples might help:
FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do @echo %i %j %k
would parse each line in myfile.txt, ignoring lines that begin with a semicolon, passing the 2nd and 3rd token from each line to the for body, with tokens delimited by commas and spaces. Notice the for body statements reference %i to get the 2nd token, %j to get the 3rd token, and %k to get all remaining tokens after the 3rd. For file names that contain spaces, you need to quote the filenames with double quotes. In order to use double quotes in this manner, you also need to use the usebackq option, otherwise the double quotes will be interpreted as defining a literal string to parse.
%i is explicitly declared in the for statement and the %j and %k are implicitly declared via the tokens= option. You can specify up to 26 tokens via the tokens= line, provided it does not cause an attempt to declare a variable higher than the letter 'z' or 'Z'. Remember, FOR variable names are case sensitive, global, and you can't have more than 52 total active at any one time.
You can also use the FOR /F parsing logic on an immediate string, by making the filenameset between the parenthesis a quoted string, using single quote characters. It will be treated as a single lineof input from a file and parsed.
Finally, you can use the FOR /F command to parse the output of a command. You do this by making the filenameset between the parenthesis a back quoted string. It will be treated as a command line, which is passed to a child CMD.EXE and the output is captured into memory and parsed as if it was a file. So the following example:
FOR /F "usebackq delims==" %i IN (`set`) DO @echo %i
would enumerate the environment variable names in the current environment.
0
Steve MeyerSystem Analyst and DeveloperAuthor Commented:
The following command did the trick.  All data files are in same directory with this batch file.

for /F "tokens=*" %%a in (FileList.txt) do robocopy Folder-A Folder-B "%%a" /r:1 /w:1 /move /s
0
Lionel MMSmall Business IT ConsultantCommented:
Glad to help, at least somewhat
0
Steve MeyerSystem Analyst and DeveloperAuthor Commented:
lionelmm, your help got me pointed in the right direction.  I use to be a pro a writing DOS batch files, but haven't done so in ten years.  Didn't even know the robocopy command existed.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Legacy OS

From novice to tech pro — start learning today.