Ad
News

18x DVD burner sales expected to catch up with 16x in 2007

Published on June 05, 2006

Demand is increasing for 18x DVD burners with sales volume in 2007 expected to catch up with that of 16x models, currently the mainstream writing speed, according to sources in Taiwan's optical disc drive (ODD) industry. Read more

Winzip unzips public beta for version 11.0

Published on October 20, 2006

Corel's Winzip division today announced that it has launched the beta version of Winzip 11.0, which is now available for public testing. New features for the latest version include increased file support, improved WAV compression, read-support for RAR and the ability to preview images in ZIP files. Read more

Microsoft releases download of Vista RC1

Published on August 29, 2006

Microsoft is offering Vista beta testers a pre-RC1 ISO download. Users can download the file and create a bootable DVD using a common DVD burning software. Read more

Blu-ray Drives Hit 8X Write Speeds

Published on September 03, 2008

A newly released internal Blu-ray drive by Buffalo Technology claims the position of world's fastest Blu-ray drive Read more

Latest Reviews & Articles

System Builder Marathon: Performance & Value

Published on October 31, 2008

Three dramatically different builds face off in a show of performance, defining the real value of each. Our mainstream system is designed to meet the needs of most users. Who should spend more and who can live with less? Read more

System Builder Marathon: $500 Gaming PC

Published on October 30, 2008

For the second to last day of our System Builder Marathon series, we add a $500 gaming PC to the mix. It's not going to be as quick as our other two builds, but we think Paul was able to get some serious value from this thing. Read more

Tom's SBM: The $1,500 Mainstream PC

Published on October 29, 2008

We're following up yesterday's $4,500 behemoth with a more affordable $1,500 mid-range build. Let's see what sort of performance (and overclocking headroom) you can get when you spend one third of the money. Read more

System Builder Marathon: The $4,500 Super PC

Published on October 28, 2008

This month's System Builder Marathon spreads the system prices out even further to $4,500, $1,500, and $500. Is today’s $4,500 system really worth three times as much as an upper-mainstream performance machine? Read more

  Tom's Hardware Forums » Windows XP » Windows XP General Discussion » Need help in writing a .bat file.
 

Need help in writing a .bat file.




Word :   Username :  
 
Bottom
Author
 Thread : Need help in writing a .bat file.
 
Profile: stranger
More Information

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!

Related Product

Register or log in to remove.

Profile: member
More Information

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.

Profile: stranger
More Information

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!

Profile: member
More Information

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.

Profile: stranger
More Information

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!

Profile: member
More Information

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

Profile: stranger
More Information

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.

Profile: member
More Information

ok found the problem. it's on the microsoft site here
http://www.microsoft.com/resources [...] x?mfr=true
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.

Profile: stranger
More Information

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:

Profile: member
More Information

should you use "xcopy" instead of "copy"? I'm probably way off base.. but might be worth a shot.

Profile: stranger
More Information

Hi.

Dunno if the syntax is different with xcopy, but I tried with the current syntax and it didn't work. :roll:

Profile: member
More Information

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

Profile: stranger
More Information

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:


  Tom's Hardware Forums » Windows XP » Windows XP General Discussion » Need help in writing a .bat file.

Go to:
 

Google Ads