Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How do I copy one file to a folder and all its subfolders in Windows 7?

For example:

file.txt 

will be copied to

folder/file.txt
folder/sub1/file.txt
folder/sub2/file.txt
share|improve this question

This example, the directory c:\prc\a\fa has a file called file1, and subdirectories a,b,c,d

The line that says for /f ......... will do it if you remove the word echo

C:\prc\a\fa>dir /b/s/ad <ENTER>
C:\prc\a\fa\a
C:\prc\a\fa\b
C:\prc\a\fa\c
C:\prc\a\fa\d

C:\prc\a\fa>for /F %f in ('dir /b/s/ad') do echo copy file1 %f <ENTER>

C:\prc\a\fa>echo copy file1 C:\prc\a\fa\a
copy file1 C:\prc\a\fa\a

C:\prc\a\fa>echo copy file1 C:\prc\a\fa\b
copy file1 C:\prc\a\fa\b

C:\prc\a\fa>echo copy file1 C:\prc\a\fa\c
copy file1 C:\prc\a\fa\c

C:\prc\a\fa>echo copy file1 C:\prc\a\fa\d
copy file1 C:\prc\a\fa\d

C:\prc\a\fa>

This FOR would also do it

C:\prc\a\fa>for /r %f in (.) do echo copy file1 %f

just remove the echo

there may be a better way than putting . in there but it works fine

C:\prc\a\fa>for /r %f in (.) do echo copy file1 %f <ENTER>

C:\prc\a\fa>echo copy file1 C:\prc\a\fa\.
copy file1 C:\prc\a\fa\.

C:\prc\a\fa>echo copy file1 C:\prc\a\fa\a\.
copy file1 C:\prc\a\fa\a\.

C:\prc\a\fa>echo copy file1 C:\prc\a\fa\b\.
copy file1 C:\prc\a\fa\b\.

C:\prc\a\fa>echo copy file1 C:\prc\a\fa\c\.
copy file1 C:\prc\a\fa\c\.

C:\prc\a\fa>echo copy file1 C:\prc\a\fa\d\.
copy file1 C:\prc\a\fa\d\.

C:\prc\a\fa>
share|improve this answer
    
Wouldn't this overwrite any files already present in these subfolders? – Paul Oct 5 '11 at 23:03
    
it's just copying a single file, into each, so the only question of whether it overwrites or not, is if any folder has a file of that name in it, will that file be overwritten. In this case you get a prompt, if it exists. if you did copy /Y it would do YES automatically, thus overwriting it. To do No automatically is more tricky.. a workaround could be you could make the copy conditional on the file not existing at the destination, so after the do in the for, you'd do if not exist %f\file1 copy file1 %f – barlop Oct 5 '11 at 23:24
    
I see. I would have thought that if you had a file c:\prc\a\fa\d\file2.txt then the for loop %f would return c:\prc\a\fa\d\file2.txt, so the copy command would be copy file1 c:\prc\a\fa\d\file2.txt, which would overwrite the file. – Paul Oct 5 '11 at 23:32
    
@paul no 'cos there's no index going 1..9 like file%f.txt copying file[1 to 9].txt and there's no *.*(star dot star) that just copies everything from source to dest. It's just a sequence of copy commands, where the source is statically file1, and the dest is one subdir after another. Try executing a FOR loop removing everything after the Do, and just saying 1 and try just saying %f. Normally a for loop loops through what's in the brackets for %f in (a b c) do echo %f . but when you do /r it's like it generates what's in the brackets that is iterated through, that is, subdirectories. – barlop Oct 6 '11 at 3:10
    
Ah of course, you are limiting the for to look for '.' which can only be folders. Cheers. – Paul Oct 6 '11 at 3:54

You can use following PowerShell script:

dir -Recurse -Directory|
    foreach { copy \Path\To\File.txt $_.FullName }

Save above in to copy1tomany.ps1 file. Press Ctrl+Win and type powershell to get the Powershell prompt. Navigate to folder and type copy1tomany.ps1.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .