[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 275
  • Last Modified:

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!
0
samiam41
Asked:
samiam41
  • 4
  • 3
1 Solution
 
Bill PrewCommented:
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
0
 
Bill PrewCommented:
Here's something a bit more expandable...

@echo off
set DirList="d:\test","d:\test1"
set DestDir=d:\home
set ExtList="doc","docx","pdf","vsd"
for %%A in (%DirList%) do (
 for %%B in (%ExtList%) do (
   copy "%%~A\*.%%~B" "%DestDir%"
 )
)

Open in new window

~bp
0
 
samiam41Author Commented:
Thanks billprew.   I will review them tonight.  
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
samiam41Author Commented:
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
0
 
samiam41Author Commented:
0
 
Bill PrewCommented:
Yes, I can explain the code a bit further shortly.

~bp
0
 
Bill PrewCommented:
@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
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now