Windows – copy file to multiple folders via batch script

cmd.exewindows

I want to copy a file (file.txt) inside all folders of a given destination. I want to create a batch file that does the job, but I'm not so skilled in Windows batch syntax.

Best Answer

You can use the advanced version of the for command available from Windows NT 4 onwards:

You need something like this in a batch file:

for /D %%f in ("%1\*") do copy "%2" "%%f\"

The batch file works as follows:

  • The first argument is the destination directory
  • The second argument is the file to be copied

The for command with the /D switch iterates over all directories in a given path (here: %1) and invokes a command on each iteration. Said command is the copy operation which copies the file into every directory.

Of course, since the batch file is only a single line you can also execute it directly on the command line. Just note that the variable for for has only a single %, then.

Related Question