ufukgun
ufukgun

Reputation: 7209

Copy to all folders batch file?

i want to copy a file (example: a.jpg) to all folders in a directory.

i need something like

copy a.jpg */a.jpg

do you have a batch file that does something like that?

(ps. i use windows)

Upvotes: 2

Views: 8313

Answers (3)

user1129251
user1129251

Reputation: 27

you can copy a file in all folders of a directory by using following command

for /F %g in ('dir /AD/B/S') DO copy c:\myfile %g

explanation:

In above command:- %g is a variable, dir /AD/B/S :- is a command to view all folders and sub folders of a directory, /F :- is a switch which we used with for command,if we want to iterate through the results of a command enclosed in bracket (in our case it is ('dir /AD/B/S'), c:\myfile:- is the file which we want to copy in all sub folders

Upvotes: 2

Helen
Helen

Reputation: 97847

You can do this using the for command with the /r switch, which is used to enumerate a directory tree. For example, this will copy the C:\a.jpg file to the C:\Test folder and all of its subfolders:

for /r "C:\Test" %%f in (.) do (
  copy "C:\a.jpg" "%%~ff" > nul
)

The for /r "C:\Test" %%f in (.) statement enumerates the C:\Test folder and all its subfolders and %%~ff returns the current folder name.

Upvotes: 5

Preet Sangha
Preet Sangha

Reputation: 65516

use the for command

for /f "tokens=*" %f in ('dir . /ad/b') do copy "a.jpg" "%f" 

Remember to use %%f instead of % when placing in a batch file

Upvotes: 5

Related Questions