Sign in with
Sign up | Sign in
Your question

Need help in writing a .bat file.

Last response: in Windows XP
Share
June 2, 2007 4:37:58 PM

I'd like to write a .bat that would copy a file to every subdirectory of a given directory, because it's so annoying to do this manually. I did some google searches and apart from finding out about the cyclic operand for (I presume this is the one I should use) but I don't know how to point subdir1, and the end e.g. subdirN.

Could someone give me an idea? Or if you don't mind, could you please take the time and write the script for me? Thanks!

More about : writing bat file

June 2, 2007 10:31:56 PM

What are you trying to do with this;
1 file to all subdirectory?
multible files to all subdirectory?
files to a directory (and subdirectory) in a diffrent location?
files to multible directory (from a list) in a number of locations?

The more information you give the more help I can give. please feel free to PM me if you prefer.
m
0
l
June 2, 2007 11:11:33 PM

I would like when executed, the script, to copy file x.txt (located in x:\wherever\) to folder x:\something\ and all it's subfolders (e.g. x:\something\1, x:\something\2, x:\something\3). So eventually every subfolder of x:\something\ will have a copy of x.txt. But those subfolders vary in names so I can't just point them all as a destination. It'll take me more time to do that than manually copy+paste the file to each subdirectory of that directory. In essence as you mentioned it 'one file to every subdirectory', although I think the script will be quite similar if it were several files to every subfolder.

Anyway, I hope I was clearer in my request this time.

Thank you taking the time to reply!
m
0
l
Related resources
June 3, 2007 12:25:18 AM

The command you need to use is the "FOR" command (check from command prompt for/?).

But would be in the format of:
FOR /R "DIRECTORY" %A IN <"FILENAME"> DO COPY "FILE LOCTION" %A
Where;
"DIRECTORY" = Directory to write to.
"FILENAME" = Full filename (including ext but no path) of the file name to write as a copy.
"FILE LOCATION"= Full path and name of file to copy.

So if I wanted to copy a file called c:\MYTEXT.TXT to all subdiectory under D:\MYDIR\ the systax would be as follows:-

FOR /R d:\mydir\ %A IN <mytext.txt> DO copy c:\mytext.txt %A

Im not sure if this can be done with long file names (have not used for a while) but could be used in a .BAT file with no problem.
m
0
l
June 3, 2007 8:59:19 AM

Hmm, it didn't do anything here (nothing I can see that is). Could you recheck if you wrote it correctly please?

Thank you very much for helping!
m
0
l
June 4, 2007 8:42:24 PM

Sorry my mistake, the correct systax is

FOR /R d:\mydir\ %A IN (mytext.txt) DO copy c:\mytext.txt %A

Note round brackets () not <>

But would be in the format of:
FOR /R "DIRECTORY" %A IN ("FILENAME") DO COPY "FILE LOCTION" %A
Where;
"DIRECTORY" = Directory to write to.
"FILENAME" = Full filename (including ext but no path) of the file name to write as a copy.
"FILE LOCATION"= Full path and name of file to copy.

P.S you can set to over wite existing files by putting a /s on the end like so

FOR /R d:\mydir\ %A IN (mytext.txt) DO copy c:\mytext.txt %A /S
m
0
l
June 5, 2007 1:24:06 PM

Hi! I tried with brackets but it still doesn't seem to copy the file. :(  I tried with various formats (nfo/txt) but doesn't seem to do its work.

By the way, to execute the command I just start the .bat file right? Want to make sure just in case I'm doing something else wrong.
m
0
l
June 5, 2007 8:13:11 PM

ok found the problem. it's on the microsoft site here
http://www.microsoft.com/resources/documentation/window...
if you are using in a batch file you need to use %%A so the line will now read:-
FOR /R d:\mydir\ %%A IN (mytext.txt) DO copy c:\mytext.txt %%A
this is due to the fact that batch files treat all text between the % simbols as the name of a varible.
m
0
l
June 5, 2007 8:44:42 PM

Hmm, I tried with the double percentage signs, but still no use. :(  Did you try it on your own computer?

Btw I guess I should thank you for the research you're making. :wink:
m
0
l
June 6, 2007 11:59:22 AM

should you use "xcopy" instead of "copy"? I'm probably way off base.. but might be worth a shot.
m
0
l
June 6, 2007 2:06:36 PM

Hi.

Dunno if the syntax is different with xcopy, but I tried with the current syntax and it didn't work. :roll:
m
0
l
June 6, 2007 9:56:53 PM

OK, Tested this on my computer and it worked fine as a one line batch file

FOR /R d:\copytest\ %%A IN (copyme.txt) DO copy d:\copyme.txt %%A

This copied the file d:\copyme.txt to d:\copytest\ and all subdirectory

In answer to Coronaz you could use Xcopy if you need to do more funky stuff (Keep owership tags, Copy according to date, etc) but for a standard copy of one file copy will work fine.

The only thing you may need to do is put the /y on the end to automatically overwrite existing files of the same name.

FOR /R d:\copytest\ %%A IN (copyme.txt) DO copy d:\copyme.txt %%A /Y
m
0
l
June 7, 2007 12:58:27 PM

Hey,

It worked! :D  Thank you sooo very much Hollett! By the way where did you read about the %A attribute, I couldn't find anything with a search engine as it ignores those characters.

Thank you once again for helping me out man! :wink:
m
0
l
November 3, 2009 2:02:35 PM

This batch file worked fine for me even for long file names--excellent. The only problem is that a folder with a space in it will not get a copy. Is there is a way to get a file copied to these type of folders also? Thanks so much. -- Craig
m
0
l
December 20, 2009 6:57:30 PM


To copy a file say, C:/whatever/file, to each folder, subfolder, sub-subfolder under x:/wherever, here is a script.

  1. # Script CopyToSeveralFolders.txt
  2. var str folderlist, folder
  3. # Go to destination folder.
  4. cd "X:/whatever"
  5. # Collect a list of all folders at all levels.
  6. lf -r -n "*" "X:/whatever" ($ftype=="d") > $folderlist
  7. # Go thru $folderlist, one folder at a time.
  8. while ($folderlist <> "")
  9. do
  10. # Get the next folder.
  11. lex "1" $folderlist > $folder
  12. # Copy file to destination folder $folder.
  13. system copy ("\""+"C:/whatever/file"+"\"") ("\""+$folder+"\"")
  14. done



Script is in biterscripting ( http://www.biterscripting.com ) . Copy and paste the script in file C:/Scripts/CopyToSeveralFolders.txt, start biterscripting, enter the following command.

  1. script "C:/Scripts/CopyToSeveralFolders.txt"




m
0
l
August 12, 2010 10:09:26 AM

Hi Guys,

I stumbled upon this forum trying to fix the same issue.
What I eventually did is this:

Create a batch file with the following line:

for /R %%i in (.) do copy "%CD%\filename.ext" "%%i"

The statement is as following:
for /R = for statement for every directory in that folder.
%%i = set variable
in (.) = set variable with every directory
do copy = well just copy
"%CD%\filename.ext" = first statement %CD% is for current directory, second is the filename you want to copy. Put this inside "" to solve the space issue in directories
second %%i = is the same variable. It remains to be the current directory. Put it inside "" to solve the space issue.

This worked for me. You can use it without %CD%\ because you are executing it from the directory the file is in.

Hope this helps anyone stumbling on this article.
m
0
l
September 29, 2012 11:26:21 AM

hollett said:
OK, Tested this on my computer and it worked fine as a one line batch file

FOR /R d:\copytest\ %%A IN (copyme.txt) DO copy d:\copyme.txt %%A

This copied the file d:\copyme.txt to d:\copytest\ and all subdirectory

In answer to Coronaz you could use Xcopy if you need to do more funky stuff (Keep owership tags, Copy according to date, etc) but for a standard copy of one file copy will work fine.

The only thing you may need to do is put the /y on the end to automatically overwrite existing files of the same name.

FOR /R d:\copytest\ %%A IN (copyme.txt) DO copy d:\copyme.txt %%A /Y


Hollet, Thanks for your guide, what if the opposite, ie of all the subfolders containing the files. xml and will copy one folder to the folder where the folder is not made anymore. Thanks before
m
0
l
!