Batch file to search and copy files specified in txt file.

December 1, 2016 at 07:58:26
Specs: Windows 7
I need to create a batch file. That can look at a text file. (Within the text file will contain filenames like 0320485.pdf 03208748.pdf and so on.) I need the batch file then to go out onto a server drive search for those filenames that are listed in the txt file and copy the physical files it found on the server drive to a local folder. The batch file needs to be able to search on a server drive aka network shared drive. I found many batch files that do generally the right process but doesn't work on a server drive.

message edited by ComputerNut


See More: Batch file to search and copy files specified in txt file.

Report •

#1
December 1, 2016 at 08:11:49
Does the server drive have a letter?

=====================

M2 Get custom script or take private lessons


Report •

#2
December 1, 2016 at 08:16:10
Yes M:


Thank you


Report •

#3
December 1, 2016 at 08:38:37
::====== script starts here ===============
::
:: Cnut.bat 2016-12-01 23:33:39.75
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

:main
for /f "tokens=* delims= " %%a in (filelist.txt) do (
dir/b/s/a-d "D:\%%a"
) >> NEWFILE

:sub1
for /f "tokens=* delims= " %%i in (NEWFILE) do (
copy "%%i" C:\MyStuff
)
goto :eof
::====== script ends here =================

=====================

M2 Get custom script or take private lessons


Report •

Related Solutions

#4
December 1, 2016 at 09:01:09
Thanks! But, problems...Here is what I entered in...M: is the server location...

::====== script starts here ===============
::
:: Cnut.bat 2016-12-01 23:33:39.75
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

:main
for /f "tokens=* delims= " %%a in (D:\Temp\filelist.txt) do (
dir/b/s/a-d "M:\%%a"
) >> NEWFILE.txt

:sub1
for /f "tokens=* delims= " %%i in (NEWFILE.txt) do (
copy "%%i" D:\Temp\FoundFiles
)
goto :eof
::====== script ends here =================

The result is newfile.txt receives only one path and filename, and no files are copied.

My filelist.txt file has two test file names.

Can you help?

message edited by ComputerNut


Report •

#5
December 1, 2016 at 22:14:45
Seems you've done a good job of tailoring to your req and reporting what it does, so I'm stumped.

=====================

M2 Get custom script or take private lessons


Report •

#6
December 2, 2016 at 20:24:28
Need to see a copy of your "base" filelist. You may need to eliminate or add some data to get the right "search criteria". (As you stated: "within the textfile, but that does not narrow things down much as to the format of its storage within the textfile.)
Second thing is optimization of speed. If you have lots of files to be copied, then if you have to run a 'dir /s' of the server for each one, you could have a very long wait for results if the server is huge. Unless the information is extremely time-critical, I would suggest running a single 'dir /b /s' of the server, then use FIND to locate your files. (This would really be good if they're all .pdf, f/e: dir /s /b m:\*.pdf>>newfile).

You could also apply 'FINDSTR /G:filelist.txt' to speed things up even further, but that takes more tweaking.
meantime please post copy of your sourcefile of names so we can see the format.


Report •

Ask Question