Parent folder: Root
Subfolder1: Has 12000 files i.e. subfolder1\885.txt
Subfolder2: Has 15000 files i.e. subfolder2\882.txt

Source file list names.ext contains 500 different names:


How to get my FOR loop to read the first line of names.ext, search for that file, copy it when found, read second line of names.ext and repeat.

SET RootDir=E:\Source
SET CopyDir=E:\Destination
IF NOT EXIST "%CopyDir%" MD "%CopyDir%"

SET CopyFname=882.txt '(Need to cater more file names rather than one such as 882.txt, 992.txt, 555.txt etc)'

FOR /D %%A IN ("%RootDir%\*") DO FOR %%B IN ("%%~A\%CopyFname%") DO ECHO F | XCOPY /Y /F "%%~B" "%CopyDir%\"
  • I can't help but think PowerShell would be the easier option for this. E.g. a simple starter to get going - pastebin.com/LnY0sNEg – HelpingHand May 13 at 22:01
  • How was names.ext made? Is it the oldest files? The newest files? The largest files? The smallest files? The files less than 14 days old? The file greater than 14 days old? The files without the archive attribute set? ... – somebadhat May 13 at 23:20

SET "_RootDir=E:\Source"
SET "_CopyDir=E:\Destination\"
SET "_LstFile=E:\Source\Listnames.log"

TITLE <NUL & TITLE %~nx0 && 2>nul >nul MKdir "%_CopyDir%"

FOR /F Tokens^=*Delims^= %%# IN ('type "%_LSTFILE%"')DO (
FOR /F Tokens^=*Delims^= %%I IN ('^<con: "%__APPDIR__%WHERE.exe" /R "%_ROOTDIR%" "%%~#"
')DO "%__APPDIR__%XCopy.exe" /Y /F /V "%%~I" "%_CopyDir%"|"%__APPDIR__%FINDSTR.EXE" /V ")")

  1. To read one (file) line by line and get the name of the file you are looking for in a root folder and its subfolders:
FOR /F Tokens^=*Delims^= %%# IN ('type "%_LSTFILE%"')DO....
  1. To effectively search for this file listed on the current line read in the previous loop, use %%~# as an argument passed to the where command along with the flag that it is used to search recursively and in your %_RootDir% folder:
..')DO FOR /F Tokens^=*Delims^= %%I IN ('"%__APPDIR__%WHERE.exe" /R %_ROOTDIR% "%%~#"
  1. Use the For /F + WHERE /T loop output as an argument to your XCOPY, thus copying the found file (where found) to its destination folder "%_CopyDir%\":
..')do XCOPY /Y /F /V "%%~i" "%_CopyDir%\"

| improve this answer | |
  • The code is working great. Only one more thing if same filename exists in subfolder can it be renamed as 855(1).txt 855(2).txt – Asad Ali Hussaini May 13 at 23:39
  • @AsadAliHussaini this will need another question, add this task to check if file exist in target directory, count files there and add properly number in (x), is more complicate that add a fill lines code in this present question. SO, add another, i've can provide this task, with more time.. – It Wasn't Me May 13 at 23:43
  • Thanks again. You saved my job. I will post another question in a while. – Asad Ali Hussaini May 13 at 23:46
  • @AsadAliHussaini thank you too for test, is nice to know that works, am sorry my limited English, and let me know about new question... – It Wasn't Me May 13 at 23:47

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.