How to copy files to special directories

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
rogier
Posts: 2
Joined: 03 Aug 2009 10:38

How to copy files to special directories

#1 Post by rogier » 03 Aug 2009 11:02

I have a picture library with more than 1000 pictures. Let's say that the location is "C:\pic_lib\" with for example these pictures:
pic001.jpg
pic002.jpg
pic003.jpg
(..).jpg

And I have some text files called
group1.txt
group2.txt
group3.txt
(..).txt

Each textfile has per line a location to a group of pictures. For example group1.txt has somethink like:
C:\pic_lib\pic001.jpg
C:\pic_lib\pic003.jpg
C:\pic_lib\pic006.jpg
etc.

I'd like to make a script to copy all the pictures that are in each text file to a new directory. The name of the directory should be the name of the text file. I already started to make something in a batch file, but this program is not doing the right think. All pictures that were listed in the text files were send to all directories.

Code: Select all

@echo off

set PIC_LIB="C:\pic_lib"
set GROUPS=group1.txt group2.txt group3.txt

for %%A in (%GROUPS%) do (
   mkdir %%~nA   
   for /F %%B in (%GROUPS%) do (
      copy %PIC_LIB%\%%B "%%~nA\%%B"
   )

)


Do you have any idea how to solve this problem?

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#2 Post by avery_larry » 03 Aug 2009 14:00

Close. The primary mistake is using %GROUPS% in the 2nd for loop, when it should be %%A. Here's untested code for you. Note a few changes in the set commands to help make sure you don't get messed up with quotes:

Code: Select all

@echo off 

set "PIC_LIB=C:\pic_lib"
set "GROUPS=group1.txt group2.txt group3.txt"

for %%A in (%GROUPS%) do (
   mkdir %%~nA   
   for /F "usebackq" %%B in ("%%~A") do (
      copy "%PIC_LIB%\%%~B" "%%~nA"
   )

)

rogier
Posts: 2
Joined: 03 Aug 2009 10:38

#3 Post by rogier » 03 Aug 2009 14:22

You code works perfectly! Thanks.

Now I see the function of the "~" in ("%%~A"). But why is it necessary to use the "~" in "%PIC_LIB%\%%~B"? I ran the code without the "~" and that also works.

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#4 Post by avery_larry » 04 Aug 2009 08:34

I use the ~ mostly just to be certain I won't ever, even if I change my inputs, get quotes inside quotes. In this case, you can be reasonably certain that your text files do NOT contain quotes, which makes the %%~B unnecessary.

One little problem -- I should have had that 2nd for statement like this:

Code: Select all

for /F "usebackq tokens=*" %%B in ("%%~A") do (


the tokens=* will allow the individual file names to contain spaces. Note also that the ~ isn't strictly needed in "%%~A"

Again, I just always like to make sure, when using variables with filenames, that I enclose the filenames (as variables) in quotes and thus any variables that might contain quotes (most notably %1 %2 etc., and %%A %%B etc.) are stripped of their quotes first.

Post Reply