Need help in writing a .bat file.

AssassiNBG

Distinguished
Jan 6, 2007
26
0
18,530
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!
 

hollett

Distinguished
Jun 5, 2001
246
0
18,710
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.
 

AssassiNBG

Distinguished
Jan 6, 2007
26
0
18,530
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!
 

hollett

Distinguished
Jun 5, 2001
246
0
18,710
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.
 

hollett

Distinguished
Jun 5, 2001
246
0
18,710
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
 

AssassiNBG

Distinguished
Jan 6, 2007
26
0
18,530
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.
 

hollett

Distinguished
Jun 5, 2001
246
0
18,710
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
 

AssassiNBG

Distinguished
Jan 6, 2007
26
0
18,530
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:
 

craigcochran70

Distinguished
Nov 3, 2009
1
0
18,510
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
 

JenniC

Distinguished
Dec 20, 2009
1
0
18,510
To copy a file say, C:/whatever/file, to each folder, subfolder, sub-subfolder under x:/wherever, here is a script.

[cpp]# Script CopyToSeveralFolders.txt
var str folderlist, folder
# Go to destination folder.
cd "X:/whatever"
# Collect a list of all folders at all levels.
lf -r -n "*" "X:/whatever" ($ftype=="d") > $folderlist
# Go thru $folderlist, one folder at a time.
while ($folderlist <> "")
do
# Get the next folder.
lex "1" $folderlist > $folder
# Copy file to destination folder $folder.
system copy ("\""+"C:/whatever/file"+"\"") ("\""+$folder+"\"")
done[/cpp]


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.

[cpp]script "C:/Scripts/CopyToSeveralFolders.txt"[/cpp]



 

jkloppenburg

Distinguished
Aug 12, 2010
1
0
18,510
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.
 

etstalker

Honorable
Sep 29, 2012
1
0
10,510


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