Computing.Net > Forums > Programming > Copying Files Using Text List

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Copying Files Using Text List

Reply to Message Icon

Name: ChampionSleeper
Date: April 15, 2009 at 23:06:06 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

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?



Sponsored Link
Ads by Google

Response Number 1
Name: reno
Date: April 16, 2009 at 01:13:53 Pacific
Reply:

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\


0

Response Number 2
Name: ChampionSleeper
Date: April 16, 2009 at 10:54:53 Pacific
Reply:

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?


0

Response Number 3
Name: reno
Date: April 16, 2009 at 22:03:56 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Copying Files Using Text List

Batch - Copy Files using .txt List www.computing.net/answers/programming/batch-copy-files-using-txt-list/18010.html

copy files of a particular date www.computing.net/answers/programming/copy-files-of-a-particular-date/14483.html

How to create a text file using FSO www.computing.net/answers/programming/how-to-create-a-text-file-using-fso/10319.html