Copy network files with matching file names from a file list using Windows Batch Script

0

I have 2 files with 1 containing certain keywords and the other containing a list of paths. I want to search the keywords from the first file list into the list of file paths, and if found then copy the files from the specified file path to a specific target folder.

First File Contents

Keyword1
Keyword2
Keyword3
Keyword4

Second File Contents

\\server\path...\Keyword1.txt
\\server\path...\Keyword1_0_1.txt
\\server\path...\Keyword2_0_1.txt
\\server\path...\Keyword2_1_9.txt
\\server\path...\Keyword3_1_0_1.txt

I have to write the Windows batch script for this purpose.

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

Sorry @pimp-juice-it I am not sure how to paste the screenshot. Hence copy-pasting the output below -

d:\Temp_Script\Script>FOR /R "D:\Temp_Script\Source\33.txt" %G IN (55*) DO ECHO "55" d:\Temp_Script\Script>CALL :FileExist "55" "D:\Temp_Script\Source\44.txt" d:\Temp_Script\Script>FOR /R "D:\Temp_Script\Source\44.txt" %G IN (55*) DO ECHO "55" d:\Temp_Script\Script>CALL :FileExist "55" "D:\Temp_Script\Source\55.txt" d:\Temp_Script\Script>FOR /R "D:\Temp_Script\Source\55.txt" %G IN (55*) DO ECHO "55" d:\Temp_Script\Script>CALL :FileExist "55" "D:\Temp_Script\Source\55 - Copy (2).txt" d:\Temp_Script\Script>FOR /R "D:\Temp_Script\Source\55 - Copy (2).txt" %G IN (55*) DO ECHO "55" d:\Temp_Script\Script>CALL :FileExist "55" "D:\Temp_Script\Source\55 - Copy.txt"

as you can see the keyword "55" exist in the UNC, but still the condition is not validating to True in the FOR loop and it is going to the next UNC directly. Below is the code -

:FileExist FOR /R "%~2" %%G IN (%~1*) DO ECHO "%~1"

Sujitkar

Posted 2018-08-07T14:30:27.810

Reputation: 1

I'd recommend to use something else instead of batch processing. One of those files must be read as many times as a lines count in the second one... PS. Use VBS script, for example... – Akina – 2018-08-07T16:18:45.057

Answers

0

You can loop through the "keyword" list once and use the iterated keyword values along with some enclosed wildcard characters as search strings i.e. *<Keyword>*. You can walk the directory tree of each UNC path value from its file list and perform the copy operation to only those that exist matching the search string "keywords".

Essentially though...

  • The first for /f loop will read each line of the string file list one by one and each line's value will be an iterated value that is passed at the first argument to the call command.
  • The second for /f loop will read each line of the UNC path file list one by one and pass it and the first argument value passed by the first for /f loop as two arguments with its call command.
  • The last for /r loop will recursively search the iterated UNC path with the iterated string value as separate arguments it was passed, and then copy over all matching files.

Batch Script

@ECHO ON

SET "strList=\\server\Folder\Path\SearchStrings.txt"
SET "pathList=\\server\Folder\Path\UNCPaths.txt"
SET "targetPath=\\server\target\folder\path"

FOR /F "USEBACKQ TOKENS=*" %%S IN ("%strList%") DO CALL :Paths "%%~S"
PAUSE
EXIT

:Paths
FOR /F "USEBACKQ TOKENS=*" %%P IN ("%pathList%") DO CALL :FileExist "%~1" "%%~P"
GOTO :EOF

:FileExist
FOR /R "%~2" %%C IN (*%~1*) DO XCOPY /F /Y "%%~C" "%targetPath%\"
GOTO :EOF

Further Resources

  • For /F

  • Call

    The CALL command will pass control to the statement after the label specified along with any specified parameters. To exit the subroutine specify GOTO:eof this will transfer control to the end of the current subroutine.

  • For /R

    FOR /R [[drive:]path] %variable IN (set) DO command [command-parameters]
    
        Walks the directory tree rooted at [drive:]path, executing the FOR
        statement in each directory of the tree.  If no directory
        specification is specified after /R then the current directory is
        assumed.  If set is just a single period (.) character then it
        will just enumerate the directory tree.
    

Pimp Juice IT

Posted 2018-08-07T14:30:27.810

Reputation: 29 425

Sujitkar - Just remove the PAUSE so it will exit when done but adjust accordingly for your specific needs as needed. Happy to help with adjustments if needed too so just explain and let me know. – Pimp Juice IT – 2018-08-08T03:49:31.630

Thanks for such a detailed solution @pimp-juice-it. But I am unable to get this to work FOR /R "%~2" %%C IN (%~1). This condition will never return True. – Sujitkar – 2018-08-08T10:33:05.867

@Sujitkar - Can you send me a screen shot or something of the output of the command when you run it or a few lines of it for a couple iterations? Maybe there is something I misunderstood with your setup so if you can help me understand then maybe I can adjust to still work for your needs. – Pimp Juice IT – 2018-08-08T10:35:21.617

@Sujitkar Also, maybe I am assuming wrong but for example if you have Keyword1 and then you have UNC server path without the file name in the file so just \\server\path... like that, and then there should be a file name with Keyword1 characters in that path which you want copied it it exist. The UNC path file list with this method should NOT contain the file names with the keywords and just the path only for this to work. Perhaps this is why it is not working if the UNC path file list has more than just the list of UNC folder path only. – Pimp Juice IT – 2018-08-08T10:46:13.813

@Sujitkar I left you a few comments between this answer and your question above so I wanted to see if you could confirm that you read all those over, and I'm also curious if this helps clarify any? I'm anxious to help you get this simple task resolved so you can check it off your list and move on with a better understanding and learn a bit at the same time. Let me know what I can do to help you understand better and/or get this simple task resolved. – Pimp Juice IT – 2018-08-09T13:00:42.313

@Sujitkar Last comment now and will wait to hear from you.... As per this screen shot https://i.imgur.com/3chEhdJ.png you can change the "Values" of the #1 green section, but do not change anything with the #2 red secion, and the PAUSE with the small orange line is optional so either remove line entirely or leave as-is. Good luck!

– Pimp Juice IT – 2018-08-09T13:05:51.287

@Sujitkar What's the status on this? – Pimp Juice IT – 2018-08-25T02:43:42.710