Script for searching multiple directories and copying files

Hello,

I need to search multiple servers and multiple directories on each server for files containing specific information in the title. I then need to copy the files that are found to a centralized location.

What I have now is a simple batch file but it isnt working the way I would like.

dir /s \\servername\shared\filename*.* dir /s \\servername\shared\*secondfilename*.* dir /s \\sername\user\*secondfilename*.* dir /s \\servername\user\filename*.* > c:\Central_Office.txt

Now I get the files locations in a text file but am not sure how to then script a copy or xcopy to use those text files.

Suggestions?
Chris PawleyAsked:
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.

NVITCommented:
Make a .bat file of this:
Note: This version uses an ECHO prefix for visual debugging reasons. When you are satisfied it should work, remove the ECHO to run it for real.
@echo off
set TargetDir=c:\target
set FNList=c:\Central_Office.txt
(dir /b /s \\servername\shared\filename*.* & dir /b /s \\servername\shared\*secondfilename*.* & dir /b /s \\sername\user\*secondfilename*.* & dir /b /s \\servername\user\filename*.*) > %FNList%
for /f "tokens=*" %%a in (%FNList%) do (
  ECHO copy "%%a" "%TargetDir%"
)

Open in new window

1

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
David Johnson, CD, MVPOwnerCommented:
dir/b will just give you the bare filename
0
NVITCommented:
@David. If you mean... w/o the path? It returns the full path for me here, as it should.
0
Hey MSSPs! What's your total cost of ownership?

WEBINAR: Managed security service providers often deploy & manage products from a variety of solution vendors. But is this really the best approach when it comes to saving time AND money? Join us on Aug. 15th to learn how you can improve your total cost of ownership today!

Chris PawleyAuthor Commented:
So it looks like the xcopy portion is not working. Do I need to personalize any of this portion?

for /f "tokens=*" %%a in (%FNList%) do (
  ECHO copy "%%a" "%TargetDir%"
)

I already changed the TargetDir at the top.

EDIT: I think I missed the notes on the original answer. Testing again without the ECHO. Standby
0
Chris PawleyAuthor Commented:
Thank you for the quick and full response. This has been tested and works exactly how I need it to.
0
NVITCommented:
Chris... I forgot to mention... This version copies each file into the same target folder. If a same-named file is a found in different folders, it will be overwritten at the target...

Which brings the 2nd point. If a same-named file is found, the script will hang, expecting a Yes/No response to overwrite the existing file. If overwriting is no problem for you, replace the code to:
copy /y "%%a" "%TargetDir%"

Open in new window

0
Chris PawleyAuthor Commented:
I am thinking XCopy may be a better solution to prevent that. if I replace Copy with Xcopy will it work?
0
NVITCommented:
COPY and XCOPY have the same /Y switch. Since the basic solution is working, just add the /Y switch to the COPY command.
0
Chris PawleyAuthor Commented:
Great, thank you!
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
Windows Batch

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.