search & copy files from sub-folders based on list to other folder

M. Saad
M. Saad used Ask the Experts™
on
hello , i wish to have your help to copy some files from Company SERVER to my computer
i have server contain many folders and inside each folder many sub-folders and inside its other sub-folders
example : Contractor is the main folder
CCC is subfoder
Letters is sub-folder
incoming is sub-folder
outgoing is sub-folder
i want code or batch  to read list of names of some files / folder listed in text file " without extension " which  exist in any of these sub-folders and copy the result to destination folder

i want to set up the source folder as main folder " Contractor"  which is available on server but the search must be in all sub-folders under the main folder

hope the questions is clear

any help would be appreciated
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
This may not be of much help, but try having a look at robocopy dos command.

Author

Commented:
Thanks Nitin for your reply, but i really dont know it , i wish if i can have script or VBA code easier
NVITEnd-user support

Commented:
> i want code or batch  to read list of names of some files / folder listed in text file " without extension " which  exist in any of these sub-folders and copy the result to destination folder

Please clarify " without extension "?
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Author

Commented:
Thanks for your concern , kindly see below example :

I have my folder structure as you can see in the sample picture “ pic1”

Folder named “TN” contain many folders and each one of this folder contain subfolders as you can see for named “X-MEL-AGI-001885 “ example “ pic2 “


Subfolder named “50160-340-P-1125” suffix with 0 and its attachments you can see in the pic 3 for example.

Subfolder named “50160-340-P-1125” suffix with 1 and its attachments you can see in the pic 4 for example.

I want to search for the file named “50160-340-P-1125” ( regardless the suffix )  and in this example all files with this name are available into main folders

 “X-MEL-AGI-001885 “ and also under folder name “X-MEL-AGI-002420” as u can see in pic 5 for example


I just need batch or anything else that can help me to perform this task.
I need to just type the name of file and then search inside this folders in its subfolder and then copy them to specific destination on my desktop.

I wish that I explained myself better and my request is clear now.

Thx in advance for your time
pic-1.jpg
pic-2.jpg
pic-3.jpg
pic-4.jpg
pic-5.jpg
NVITEnd-user support

Commented:
Try...
ROBOCOPY "\\server\sharename\engineering\client\IK" "c:\destinationfolder" *50160-340-P-1125* /E /DCOPY:T

Open in new window

Author

Commented:
update :
i just made batch file for this command , and i have try it
it copy everything in the folder named " TN" not only the file which i need
can you please check that ?




sorry , i am using Company's PC , and i cant use it here.
can it be like batch ?
NVITEnd-user support

Commented:
> ...it copy everything in the folder named " TN" not only the file which i need
It works fine here. Except it also copies the empty folders. Here is a version that removes the empty folders.
set SrcDir=\\server\sharename\engineering\client\IK
set TgtDir=c:\destinationfolder
set CopyPattern=*50160-340-P-1125*
ROBOCOPY "%SrcDir%" "%TgtDir%" "%CopyPattern%" /E /DCOPY:T
for /f "tokens=*" %%a in ('dir /b /s /ad "%TgtDir%" ^| sort /r') do (
  rd /q "%%a"
)

Open in new window

NVITEnd-user support

Commented:
> i want code or batch  to read list of names of some files / folder listed in text file " without extension " which  exist in any of these sub-folders and copy the result to destination folder

Please provide example text file.

Author

Commented:
i have attached sample for requied documents which i need to search under folder named "TN".

please note that each single number of these number have many copies in different sub-folders under main folder named "TN".
for example documnet no "50160-340-P-1125 " is available in following format :

50160-340-P-1125_0_0010.pdf
50160-340-P-1125_0_0020.dox
50160-340-P-1125_1_0010_Marked.pdf
50160-340-P-1125_2_0020.pdf
50160-340-P-1125_2F1_0020.pdf
50160-340-P-1125_4_0010.pdf

so , if i just add the documnet number " 50160-340-P-1125 "  into text file , i wish to have copy of all the above documents from all sub-folder under main folder named "TN".

i will search for many different numbers into text file not only 1 document number everytime.

wish its cleare for you & thanks in advance
sample.txt
End-user support
Commented:
Thanks for that clarification.

Here is a tested and enhanced version of my prior code post.
Before using, please revise the values of these variables to your need: SrcDir, TgtDir, FileList

@echo off

set SrcDir=\\server\sharename\engineering\client\IK
set TgtDir=c:\destinationfolder
set FileList=c:\sample.txt

REM Copy desired files
for /f "tokens=*" %%a in ('type "%FileList%"') do (
  ROBOCOPY "%SrcDir%" "%TgtDir%" "*%%a*" /E /DCOPY:T
)

REM Since robocopy copies empty folders, we need to remove those.
for /f "tokens=*" %%a in ('dir /b /s /ad "%TgtDir%" ^| sort /r') do (
  rd /q "%%a"
)

Open in new window

Author

Commented:
Hello , thanks  a lot for your message , I have tested the batch many times and its very fast &good I just have 2 small issue if you can fix for me :

This batch work very good with the documents available on my PC but sometimes create empty folder which is not related to my search criteria.

Also if I wanted to search for document available on my Company’s server its not working at all, I have changed the path for “SrcDir” as you mentioned in your batch start with “\\10.10 etc “ but without positive result.
Would you please check this for me.

Thanks again for your time & effort
NVITEnd-user support

Commented:
> I have tested the batch many times and its very fast
This being true, question should be closed and points awarded.

> I just have 2 small issue if you can fix for me :
This could be small or big. I can't tell without putting in more time. Please open another question.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial