0
for /r "C:\Users\bui\Desktop\Annotation Traing Package\test1" %i in (*03.json)  do copy "%i" "C:\Users\bui\Desktop\Annotation Traing Package\test2"

i want to copy all 03.json files from test1 to test2 but in test1 folder, there are subfolders, and in those subfolders also have 03.json files. With for loop i cant loop through subfolders and copy 03.json files in it, but i also want to copy those subfolders from test1 to test2 with those 03.jon files in it. is there possible way to do it?

i tried using xcopy to copy those subfolders first then use for loop but no use, bc they copy all files to "C:\Users\bui\Desktop\Annotation Traing Package\test2"

4
  • Please open a command prompt, run robocopy /? and read the output help. There can be used robocopy "%USERPROFILE%\Desktop\Annotation Traing Package\test1" "%USERPROFILE%\Desktop\Annotation Traing Package\test2" *03.json /S /NDL /NFL /NJH /NJS /R:1 /W:1 >nul to copy all *03.json files in entire directory tree of test1 to test2 with replicating the directory tree. Commented Dec 21, 2022 at 6:25
  • @Mofi thank you for your answer, the command work perfectly, but my boss keep telling me that add if command to the for loop could make it work aswell. You got any ideal how to do it? Commented Dec 21, 2022 at 8:21
  • 1
    Ask your boss to sbow you then! If they aren't just pulling your leg, and they do show you, you can post their response, and we'll see how clever they really are. Just be aware that the example already commented is unlikely to be improved upon for simplicity, command length, or efficiency. Commented Dec 21, 2022 at 11:04
  • In a command prompt window can be used also for /R "%USERPROFILE%\Desktop\Annotation Traing Package\test1" %I in (*03.json) do @for /F "tokens=6* delims=\" %J in ("%~dpI") do @md "%USERPROFILE%\Desktop\Annotation Traing Package\test2\%K" 2>nul & copy /B /Y "%I" "%USERPROFILE%\Desktop\Annotation Traing Package\test2\%K" >nul which is less efficient than the usage of ROBOCOPY designed for exactly such file copying tasks. BTW: The folder name Annotation Traing Package should be most likely Annotation Training Package. Commented Dec 22, 2022 at 6:22

1 Answer 1

0

RoBoCopy is the best method to do this IMHO.

Robocopy "C:\Users\bui\Desktop\Annotation Traing Package\test1" "C:\Users\bui\Desktop\Annotation Traing Package\test2" *03.json /E /S /B

The Robocopy utility is made to copy directory trees and matching files and has been included in windows since Windows 7 released in 2009.

Note I used /E assuming you would want the folders which are empty assuming you may have things like log folders which contain no "03.json" files

If you do not want to create empty directories when they do not contain "03.json" files, then simply omit the /E option

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.