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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

NVITEnd-user supportCommented:
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

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
NVITEnd-user supportCommented:
@David. If you mean... w/o the path? It returns the full path for me here, as it should.
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

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
Chris PawleyAuthor Commented:
Thank you for the quick and full response. This has been tested and works exactly how I need it to.
NVITEnd-user supportCommented:
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

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