Copying Files Using Text List

April 15, 2009 at 23:06:06
Specs: Windows XP
I have a text list of about 3000 numbers/names that correspond (only partially) to file names in a folder/subfolders that need to be compiled into one new folder (files only; not directory structure).

I would like to search the main folder and subfolders for all hits to these numbers/names, and create a list of file names/paths.

Then, using the list of paths, I would like to move the listed files to a new folder.

I have had success with the first portion of the task (searching and listing file names). The code I am using is below (searchtext.txt contains the numbers to search).

for /f %%a in ('type "C:\searchtext.txt"') do dir /b /s |Find "%%a" >> C:\files2move.txt

I am having trouble with the second task(copying the listed files to a new folder). What I have tried is below:

for /f %%a in ('type "C:\files2move.txt"') do xcopy "%aa" C:\newfolder

When I run this code I get a parse error. I have also tried the move command. The file/folder names/paths in the list are long and contain spaces. I know this might be a problem, but I am not sure how to fix this.

Any ideas?

See More: Copying Files Using Text List

Report •

April 16, 2009 at 01:13:53
for /f "tokens=*" %%a in (C:\searchtext.txt) do (
dir/b/s "%%a" 1>>C:\files2move.txt 2>nul

if not exist c:\newfolder\ md c:\newfolder
for /f "tokens=*" %%a in (C:\files2move.txt) do copy "%%a" C:\newfolder\

Report •

April 16, 2009 at 10:54:53
Thanks for the quick response, Reno.

#1: Doesn't work for me. I am not sure why. What is 2>nul for?

#2: Works like a charm(using my original code to generate file list).

A few more questions, I would like to eliminate/select for some of the search results based on prefixes and extensions. Examples:
002-102-214-5_V (wanted)
V002-102-214-5_V (not wanted)
ASC002-102-214-5 (not wanted)

214567.pdf (wanted)
214567.prt (not wanted)

I am not sure what will be the best way to do this. I could use xcopy and exclude, but I am not sure that will allow me to exclude prefixes like the V above without also eliminating some wanted results. Also, I know the extensions I want but not the extensions I don't want. Any ideas?

Report •

April 16, 2009 at 22:03:56
1. the dir part works here, it eliminate the find inside loop which is slow in execution speed.
2>nul is to supress error message eg. file not found.
try the below code to see the different
dir/b notexist.txt
dir/b notexist.txt 2>nul

3. outside a loop, use findstr/v /b "V" to eliminate V prefixes.
for inside a loop, if the prefix length is known, substring is faster than echo %%a|findstr.
set f=%%a
if/i "%f:~,1%"=="V" echo exclude %%a

to filter extension:
if/i "%%~xa"==".prt" echo exclude %%a

Report •

Related Solutions

Ask Question