Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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?

1.
for /f "tokens=*" %%a in (C:\searchtext.txt) do (
dir/b/s "%%a" 1>>C:\files2move.txt 2>nul
)2.
if not exist c:\newfolder\ md c:\newfolder
for /f "tokens=*" %%a in (C:\files2move.txt) do copy "%%a" C:\newfolder\

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?

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>nul3. 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 %%ato filter extension:
if/i "%%~xa"==".prt" echo exclude %%a

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |