Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I need the syntax of a simple batch file that will copy a file from a root folder to all subfolders (not recursive) that start with a prefix.

E.g.: I need the batch file c:\MyRoot\CopyCfgToSubs.bat with a for loop that will copy the file c:\MyRoot\My.config
into each of the sub folders:
c:\MyRoot\Mysub1\
c:\MyRoot\Mysub2\
.
.
.
c:\MyRoot\MysubN\

[sup]
C#
// What have I tried?

I've come across this code sample:
VB
for /r "C:\MyRoot" %%f in (.) do (
  copy "C:\MyRoot\a.config" "%%~ff" > nul
)


Which is 80%, what I need in addition is how to copy to the 1st level of subfolders only, AND only to those subfolders that start with "MySub"
[/sup]
Posted
Updated 25-Mar-15 23:17pm
v4
Comments
Richard MacCutchan 26-Mar-15 4:42am    
What have you tried, and where are you stuck? You can type "help for" at a command prompt to see how to process a list of file or directory names.
Joezer BH 26-Mar-15 4:44am    
As it's a one line solution I thought the "what have you tried" can be avoided.
It might perhaps be that I was mistaken

Added above some "what have I tried"
Herbisaurus 26-Mar-15 4:54am    
NOTE: The batch sample you provided will copy the file also to the root level...
Joezer BH 26-Mar-15 5:34am    
Yes, I understand that. Plus, and it will also copy it into all the sub folders of the sub folders etc... which is what I want to prevent

1 solution

In CMD it's Something like:
VB
cd \MyRoot
for /D %x in (MySub*) do copy a.config %x

and inside a batch file:
VB
cd \MyRoot
for /D %%x in (MySub*) do copy a.config %%x
 
Share this answer
 
v3
Comments
Joezer BH 26-Mar-15 5:55am    
"x was unexpected at this time"

??
Richard MacCutchan 26-Mar-15 6:03am    
If you type it in at the command prompt it uses %x, if it is in a batch file then it needs %%x.

As a test you could type
for /D %x in (MySub*) do echo copy a.config %x
at the command prompt to check if it's correct.
Joezer BH 26-Mar-15 6:14am    
V - tnx Richard

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900