PDA

View Full Version : XP Batch File: copy a file to multiple directories in a loop


Wamplet
09-05-2006, 03:53 PM
I was wondering if anyone could help a brotha out:


I want to copy a file to a set of folders, such as

copy c:\crap\crapfile c:\crapfolder\a
copy c:\crap\crapfile c:\crapfolder\b
copy c:\crap\crapfile c:\crapfolder\c
copy c:\crap\crapfile c:\crapfolder\d
copy c:\crap\crapfile c:\crapfolder\e
etc.

I have been trying to look this up on the intarweb and have been working a little with batch files the past 2 days, but I have unfortunately been busier than a 3-peckered goat today, so I had no chance to even look for some help. :(

From what I was working on so far, I believe I will need to use "FOR" and possibly a loop of some sort if it's not covered by FOR, but I am not quite sure on the exact syntax and how i will assign a variable such as the drive and increment it within a batch file. :o

Help me Obi Wan... You're my only hope...

*end transmission*

Mike359
09-05-2006, 04:00 PM
Do you mean copy all files from one folder and give each file a sepreate folder?
I would use a loop and the FOR command.
This page helped me SO MANY TIMES http://www.cs.ntu.edu.au/homepages/bea/home/subjects/ith305/description.html

Wamplet
09-05-2006, 04:12 PM
Do you mean copy all files from one folder and give each file a sepreate folder?

I want to take one file and copy it to folder a, b, c, d, etc (which don't exist yet).

Here is what I just wrote real quick and it sort of works. :O


FOR %%A IN (1 2 3) DO COPY C:\b\a.a C:\b\%A

The test file is located in C:\b called a.a

I want to have the batch file make a new directory based on the 3 numbers above.

so it will make c:\b\1 and then copy a.a to it, repeat for c:\b\2 and c:\b3

It looks like i forgot to put in the md command, but i will get to that later when i get a chance. I just wanted to write a quick line of what i think needs to be written.

I am not sure if i can have a MD c:\b\1 (is that %A ??? :confused:) on the same line as the copy portion. Of course, MD will need to be done first.

The line above I tested out real quick and all it did was copy a.a to the same directory it was stored in but copied it as A.

So it basically did this: copy c:\b\a.a c:\b\ but it created a file called A and rewrote over itself 2 more times. :o

Jiminator
09-05-2006, 09:54 PM
have the for call a second batch program that consists of
md %1
copy %2 %1

or something like that. its been a long time since I used dos

Hendricks266
09-05-2006, 10:30 PM
Make a file called loopdirc.bat. Make it look like this:

@echo off
cls

md c:\crapfolder\a
md c:\crapfolder\b
md c:\crapfolder\c
md c:\crapfolder\d
md c:\crapfolder\e

copy %cd%\%%1 copy c:\crapfolder\a\%%1
copy %cd%\%%1 copy c:\crapfolder\b\%%1
copy %cd%\%%1 copy c:\crapfolder\c\%%1
copy %cd%\%%1 copy c:\crapfolder\d\%%1
copy %cd%\%%1 copy c:\crapfolder\e\%%1

goto :eof

All you have to do is input this function anywhere:

for %%i in (*.*) do call loopdirc.bat %%i

Untested though. If it doesn't work try changing the all %% to %.

Wamplet
09-06-2006, 09:26 AM
I'll try this out later today if i get a chance and let you know how it goes.

Thanks for the help everyone.

diamondblast
12-19-2008, 02:28 PM
I'll try this out later today if i get a chance and let you know how it goes.

Thanks for the help everyone.

promises promises... how did it go?
I think there must be something more elegant :) than this...
anyway. let us know if it's working?