Windows Batch commands to copy file to multiple subdirectories

batchcommand linefile-transferwindows

I have 2 files file1.txt and file2.txt that I want to copy into a folder structure like this

parentfolder
    subfolder1
         destinationfolder
    subfolder2
         destinationfolder
    subfolder3
         destinationfolder
    subfolder4
         destinationfolder
    subfolder5
         destinationfolder

where file1.txt and file2.txt go into the destinationfolder directory. Also the names of the subfolder(NUM) are unique and nonsequential.

Is there a way to automate this copy and paste task?

Best Answer

Since the folders are unique and non-sequential, I will assume you know in advance what they are.

You can use xcopy for this or robocopy

This is with xcopy

xcopy "c:\myfile.txt" "c:\parent\subfolder1\" /z /i
xcopy "c:\myfile.txt" "c:\parent\subfolder2\" /z /i 
xcopy "c:\myfile.txt" "c:\parent\subfolder3\" /z /i
xcopy "c:\myfile.txt" "c:\parent\subfolder4\" /z /i
xcopy "c:\myfile.txt" "c:\parent\subfolder5\" /z /i

xcopy "c:\myfile2.txt" "c:\parent\subfolder1\" /z /i
xcopy "c:\myfile2.txt" "c:\parent\subfolder2\" /z /i
xcopy "c:\myfile2.txt" "c:\parent\subfolder3\" /z /i
xcopy "c:\myfile2.txt" "c:\parent\subfolder4\" /z /i
xcopy "c:\myfile2.txt" "c:\parent\subfolder5\" /z /i

Save the code to notepad, and save as. Name it Copy.bat (note the .bat), Run it. All done.

Related Question