copy files if their names exist in a txt file

September 26, 2011 at 08:46:08
Specs: Windows 7
Hi gurus,

This is my first time trying to do anything in old good DOS, as it looked simplest of all available solutions for my task.
Here is what I need to acomplish. I have a coma separated list of file names stored in a document on my C: drive. What I need to do is Read the names listed in that document lets call it List.txt . For every filename in List.txt I need to copy the file which will be under my C drive ( subfolders A,B,C or D) and once found I would like the file to be copied over to C:\ NewFolder.
Loop and do the same for every file name in my List.txt.

Thanks in advance

See More: copy files if their names exist in a txt file

Report •

September 27, 2011 at 06:08:28
Does list.txt contain the full path to the files or just the file names?

Report •

September 27, 2011 at 07:16:02
Only the names.
Thanks Jowah.

Report •

September 27, 2011 at 09:57:27
Ok in that case I think we would have to search each directory for each file. Something like this:

@echo off

for /f "tokens=* delims=," %%A in (list.txt) do (
dir c:\A\%%A > a.tmp
dir c:\B\%%A > b.tmp
dir c:\C\%%A > c.tmp
dir c:\D\%%A > d.tmp
REM setting variables with the results of our dir statements above
set /p aresult=<a.tmp
set /p bresult=<b.tmp
set /p cresult=<c.tmp
set /p dresult=<d.tmp
REM checking our variables to see if they contain something other than "File Not Found", and if they do, copying it to c:\NewFolder
if aresult neq "File Not Found" copy /q c:\A\%%A c:\NewFolder\
if bresult neq "File Not Found" copy /q c:\B\%%A c:\NewFolder\
if cresult neq "File Not Found" copy /q c:\C\%%A c:\NewFolder\
if dresult neq "File Not Found" copy /q c:\D\%%A c:\NewFolder\

Report •

Related Solutions

Ask Question