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


Second File Contents


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"

  • 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 Aug 7 '18 at 16:18

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


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"

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

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

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.
  • 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 Aug 8 '18 at 3:49
  • 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 Aug 8 '18 at 10:33
  • @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 Aug 8 '18 at 10:35
  • @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 Aug 8 '18 at 10:46
  • @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 Aug 9 '18 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.