Home > Software engineering >  using a batch to copy single file into subfolders and their subfolders and so on
using a batch to copy single file into subfolders and their subfolders and so on

Time:03-25

I'm curious if that task a have to perform is achievable by using a batch. I have to copy a single file into multiple subfolder which have also subfolders the file has to be copied to as well. Using a batch, the input would look something like this:

@echo off
for /d %%a in ("C:\\Users\\thomas\\Desktop\\Battest\*.\*") do xcopy /y /d C:\\Users\\thomas\\Desktop\\Battest\\indextest.php "%%a"

This would only copy the index.php into the first subfolder, which are directly on the next level.
Since the index.php needs to be copied into 700 folders and I don't know all the concrete directory paths, I'm not sure how to handle this using a wildcard or if this task is even achievable like this.

I've tried to utilize a batch to copy a single file into multiple subfolders, which path I don't know. So far I just managed to copy the file on level.

CodePudding user response:

Can't you do that with forfiles? Something like (not tested):

FORFILES /C "cmd /c if @isdir==TRUE xcopy /y /d C:\Users\...\indextest.php @path"

CodePudding user response:

for /r "C:\Users\thomas\Desktop\Battest" %%a in (.) do ECHO xcopy /y /d C:\Users\thomas\Desktop\Battest\indextest.php "%%a"

Should echo the copy command for each subdirectory of the directory nominated after the /r.

Obviously, remove the echo keyword after verification to actually execute the xcopy.

See for /? from the prompt for details.

  • Related