Copy a file to all folders batch file?

16,041

Solution 1

Give this a try:

for /r "D:\Software\destinationfolder" %i in (.) do @copy "C:\Users\meotimdihia\Desktop\credits.jpg" "%i"

Of course, if it's to go into a batch file, double the '%'.

Solution 2

This was the first search I found on google for batch file copy file to all subfolders.

Here's a way with xcopy. There's also robocopy but that would be too powerful for a simple task like this. (I've used that for entire drive backups because it can use multi-threading)


But, let us focus on xcopy.

This example is for saving to a file with the extension .bat. Just drop the additional % where there is two if running directly on the command line.

cd "D:\Software\destinationfolder"
for /r /d %%I in (*) do xcopy "C:\temp\file.ext" "%%~fsI" /H /K
  • cd "D:\Software\destinationfolder" change directory to the folder you want to copy the file to. Wrap this in quotes if the path has whitespaces.
  • the for loop - See help here. Or type for /? in a command prompt.
  • /r - Loop through files (recurse subfolders)
  • /d - Loop through several folders
  • %%I - %%parameter: A replaceable parameter
  • xcopy - Type xcopy /? in the command line for lots of help. You may need to press Enter to read the entire help on this.
  • C:\temp\file.ext - The file you want to copy
  • "%%~fsI" - Expands %%I to a full pathname with short names only
  • /H - Copies files with hidden and system file attributes. By default, xcopy does not copy hidden or system files
  • /K - Copies files and retains the read-only attribute on Destination files if present on the Source files. By default, xcopy removes the read-only attribute.

The last two parameters are just examples if you're having trouble with any read-only files and will retain the most important file properties.

Lots more xcopy parameters here

xcopy examples here


Just for completeness. This example below will copy the same file in each folder of the current directory and not any sub-folders. Just the /r option is removed for it to behave like this.

for /d %%I in (*) do xcopy "C:\temp\file.ext" "%%~fsI" /H /K

Solution 3

If you can use it: Here is a PowerShell solution (PowerShell is integrated in Windows 7 and available from XP and up):

$file = "C:\...\yourfile.txt"
$dir = "C:\...\YourFolder"

#Store in sub directories
dir $dir -recurse | % {copy $file -destination $_.FullName}
#Store in the directory
copy $file -destination $dir

I'm pretty sure that the last line can be integrated in dir ... but I'm not sure how (I do not use PowerShell very often).

Share:
16,041
meotimdihia
Author by

meotimdihia

Updated on June 14, 2022

Comments

  • meotimdihia
    meotimdihia about 2 years

    I need copy credits.jpg from C:\Users\meotimdihia\Desktop\credits.jpg to D:\Software\destinationfolder and all subfolders I read many and i write

    /R "D:\Software\destinationfolder" %%I IN (.) DO COPY "C:\Users\meotimdihia\Desktop\credits.jpg" "%%I\credits.jpg"
    

    then i save file saveall.bat but i run it , it dont work at all. help me write 1 bat

  • Piedone
    Piedone over 3 years
    Works like a charm and super simple.