Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy a file to all folders batch file?

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

like image 288
meotimdihia Avatar asked Sep 02 '10 18:09

meotimdihia


People also ask

How do I copy a file to multiple folders at once?

If you need to copy a file to multiple folders, you can hold down the Ctrl key, and drag the file or folder on to each folder you want to copy it to.

What is %% in a batch file?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.


3 Answers

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 '%'.

like image 159
BillP3rd Avatar answered Oct 07 '22 15:10

BillP3rd


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

like image 45
Ste Avatar answered Oct 07 '22 17:10

Ste


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).

like image 43
Lasse Espeholt Avatar answered Oct 07 '22 16:10

Lasse Espeholt