Link to home
Start Free TrialLog in
Avatar of samiam41
samiam41Flag for United States of America

asked on

Copy any file that has particular extensions in two different directories

Hi Experts.  Ok, I could probably (eventually) figure this out but because of the time crunch, I am asking the 4th deminsional experts on here.

I have two directories (d:\test and d:\test1) that contain various files but I only need to copy the files with these possible extensions (.doc, .docx, .pdf, .vsd) to the destination directory d:\home.  I am using a batch script to run this (also I suck at scripting thus another reason I am trying to keep it easy).  

I appreciate your help!
Avatar of Bill Prew
Bill Prew

Here's the brute force approach.  Let me know if you were looking for something more elegant...

@echo off
copy d:\test\*.doc d:\home
copy d:\test\*.docx d:\home
copy d:\test\*.pdf d:\home
copy d:\test\*.vsd d:\home
copy d:\test1\*.doc d:\home
copy d:\test1\*.docx d:\home
copy d:\test1\*.pdf d:\home
copy d:\test1\*.vsd d:\home

Open in new window

~bp
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of samiam41

ASKER

Thanks billprew.   I will review them tonight.  
Great work as always.  I know you may not have time now but as you have a second (or two), I would really like to learn just a little bit from this exercise.  What does the %%A and %%B mean and why did you use them?  I see they act as some sort of condition for the script but knowing "why" you used them may help me from asking the same sort of question on here several times.  Again, I know you are ubber busy so as time permits, even a link that would help explain would be appreciated.

Thanks again for your help!

-Aaron
Yes, I can explain the code a bit further shortly.

~bp
@echo off

Turn off echoing of each command executed in the script

set DirList="d:\test","d:\test1"

Define variable DirList to contain a quoted comma delimited list of the folder to process

set DestDir=d:\home

Define variable DestDir to specify where the files schould be copied to

set ExtList="doc","docx","pdf","vsd"

Define variable ExtList to contain a quoted comma delimited list of the extensions to process

for %%A in (%DirList%) do (

Outermost loop will process the folders in the list one at a time.  Each time through the list the %%A loop variable will take on the value of the current list item.

To get a sense for how this works, try the following small test at a command line prompt:

for %A in (1 2 3) do @echo %A

(notice that when doing a FOR command at a command prompt the loop variables are referenced via %A rather than %%A)

Loop variables in a batch file are always named %%A through %%Z or %%a through %%z and are case sensitive.  There are also some additional modifiers that can be used to reference the loop variables.  As a simple example of this try these two lines at a command prompt and notice how the ~ removes the surrounding quotes from the loop values.

for %A in ("1" "2" "3") do @echo %A
for %A in ("1" "2" "3") do @echo %~A

For more info on the FOR command (and any other command for that matter) do FOR /? at a command prompt.  Additional ionfo can be found at:

http://ss64.com/nt/for.html

for %%B in (%ExtList%) do (

Here we do a nested FOR loop, Looping over the extension list processing it one element at a time.  Since this loop is inside the first loop and it i using %%A as its loop variable, we pick a different loop variable for this FOR, in this case %%B.

  copy "%%~A\*.%%~B" "%DestDir%"

Now we build the actual copy command we want to perform.  The from file name is "%%~A\*.%%~B" and the %%~A will be replaced with the current directory name from DirList we are processing, and %%B will be replaced by the current extension from ExtList that we are processing.

The destination for the file copy will be "%DestDir%" and %DestDir% resolves to the value of the DestDir variable, in this case d:\home

)

End the block of code associated with the second FOR loop.

)

End the block of code associated with the first FOR loop.

~bp