# Windows Batch commands to copy file to multiple subdirectories

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?

• Do all destinationfolders have the same name? – Karan Jun 19 '15 at 15:12
• Yes, the destinationfolders are all the same name, just the subfolder names are unique – wjason94 Jun 19 '15 at 17:22

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.

• Thanks for the quick reply! Looks like I will have to hardcode the subfolder names in the script. Is there not a way to use wildcards to indicate the subfolder name? The subfolder names are like "abc" , "xyz" , "ohi" , etc. – wjason94 Jun 19 '15 at 14:45
• No, you can't do that but there are some work arounds - stackoverflow.com/questions/8609028/… – Dave Jun 19 '15 at 14:47

This batch file should take care of your requirement once you edit the paths and folder names properly:

@echo off
for /r "drive:\path\to\parentfolder" %%d in (.) do (
if "%%~nd"=="destinationfolder" (
xcopy "drive:\path\to\file 1.txt" "%%~d"
xcopy "drive:\path\to\file 2.txt" "%%~d"
)
)

• Is the "%%~nd" supposed to mean if any directory inside parentfolder has a destinationfolder directory? Does the ~ indicate to go 1 level deeper into the directory? – wjason94 Jun 19 '15 at 20:26
• "Is the "%%~nd" supposed to mean if any directory inside parentfolder has a destinationfolder directory?" - Yes. "Does the ~ indicate to go 1 level deeper into the directory?" - No, it just removes the quotes. See for /? for more. Why do you ask? Do you have other subfolders of parentfolder that also have destinationfolder inside but where you do not want the files to be copied? – Karan Jun 19 '15 at 22:47

I had the same issue here, Karan gave a good answer, thanks! For me what I needed was to remove this part

if "%%~nd"=="destinationfolder"


for it to work for me. The If statement as stated on the last reply seeks specific folders if you put on the "destinationfolder", removing this line will target all folders inside the parent folder, which is what I wanted in my case.