Monday, March 26, 2018

windows - copy file to multiple folders via batch script



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.


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.


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...