Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Batch File to Copy Files based on filenames listed in text file.

Posted on 2011-10-27
46
Medium Priority
?
348 Views
Last Modified: 2012-08-14
I have a folder with 2000 jpg image files and a text file that lists out about 900  filenames  that I need to have copied over to a destination folder.   The text file does not have the path, just the file name.  

It seems simple, but I am unfamiliar with what the batch file syntax does. I've seen solutions that use for /F "delims-"%%a    and other that use for /f %%a......

Please specify whether the solution should be run in cmd.exe vs creating a .bat file.
I just need something that works.

Thanks.
0
Comment
Question by:fedexpert
  • 13
  • 11
  • 8
  • +3
46 Comments
 
LVL 10

Expert Comment

by:effx
ID: 37040589
do you have the option of using VBS?
0
 

Author Comment

by:fedexpert
ID: 37040609
No, that is not available to me.
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37040875
This should get you on your way.

Set your source and destination locations in line 2 and 3 and set the filename of your text file in line 4.
@echo off
set source=c:\source
set destination=c:\destination
set files=%source%\file.txt

for /f "tokens=*" %%a in ("%source%\file.txt") do (
   copy /y "%source%\%%a" "%destination%" >nul
)

Open in new window

Copy the code into Notepad. Edit lines 2, 3 and 4. Save it as a batch file. And you're good to go!

0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 10

Assisted Solution

by:ReneGe
ReneGe earned 248 total points
ID: 37040879
@ECHO OFF

SET Source=C:\Source
SET Dest=C:\Destination
SET FileNameList=FileNameList.txt

IF NOT EXIST "%Dest%" MD "%Dest%"

FOR /R "%Source%" %%A IN (*.jpg) DO FINDSTR -i "%%~nxA" "%FileNameList%" && COPY "%%~fA" "%Dest%"

PAUSE
0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 248 total points
ID: 37040883
OK, so you have, say c:\files.txt with in it

file1.jpg
file2.jpg
file3.jpg

And these are all in c:\sourcedir and need to go to c:\destdir say?

@echo off
cd /d "c:\sourcedir"
for /f "delims=" %%a in ('type c:\files.txt') do copy "%%~a" c:\destdir

If you want to type it into a cmd.exe promtp without saving as .bat or .cmd file then change the %% for a single %

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37040892
Three at exactly the same time... that must be a record... Billprew / billDL you disappoint me on not making it 5 :-)

Steve
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 37040911
Coool!
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37040914
Oops... Posted again due to an error:

Set your source and destination locations in line 2 and 3 and set the filename of your text file in line 4.
@echo off
set source=c:\source
set destination=c:\destination
set files=%source%\file.txt

for /f "tokens=*" %%a in ("%files%") do (
   copy /y "%source%\%%a" "%destination%" >nul
)

Open in new window

Copy the code into Notepad. Edit lines 2, 3 and 4. Save it as a batch file. And you're good to go!
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 37041010
Inspired by Paul's script, added /Y to copy
 
@ECHO OFF

SET Source=C:\Source
SET Dest=C:\Destination
SET FileNameList=FileNameList.txt

IF NOT EXIST "%Dest%" MD "%Dest%"

FOR /R "%Source%" %%A IN (*.jpg) DO FINDSTR -i "%%~nxA" "%FileNameList%" && COPY /y "%%~fA" "%Dest%"

PAUSE

Open in new window


If you want to preserve the source path:
 
@ECHO OFF

SET Source=C:\Source
SET Dest=C:\Destination
SET FileNameList=FileNameList.txt

IF NOT EXIST "%Dest%" MD "%Dest%"

FOR /R "%Source%" %%A IN (*.jpg) DO FINDSTR -i "%%~nxA" "%FileNameList%" && (
	IF NOT EXIST "%Dest%%%~pA" MD "%Dest%%%~pA"
	copy /y "%%~fA" "%Dest%%%~pA"
)

PAUSE

Open in new window

0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 37041174
@Steve

==> Billprew / billDL you disappoint me on not making it 5 :-)

Sorry dude, the "bills" are paying the "bills"...

~bp
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 37041186
@Paul

Won't you need usebackq ?

~bp
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 37041213
Not too many ways to skin this one, but seems like a single command at a command prompt (not in a BAT file) could do the job:

for /F "usebackq tokens=*" %F in ("c:\filelist.txt") do @copy "c:\fromdir\%~F" "c:\todir"

Open in new window

~bp
0
 

Author Comment

by:fedexpert
ID: 37041214
Thanks for the quick response all.  @PaultoMasi - I wasn't able to get your code to copy to the destination folder.  I was only able to get dragonit and ReneGe's to work.  

@Dragonit @ ReneGe - thanks for your assistance.  

Is there a way to have it rename the files as well or should that be a separate script? Thought?
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 37041231
How would you want them renamed?

~bp
0
 

Author Comment

by:fedexpert
ID: 37041260
Well, for some reason the file has 2-3 extraneous characters at the end.

So, I would only want the first 6 characters.

So for 12345678.jpg, I would rename to 123456.jpg
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 37041271
Yes.

The following will add _Backup to the file name.

@ECHO OFF

SET Source=C:\Source
SET Dest=C:\Destination
SET FileNameList=FileNameList.txt

IF NOT EXIST "%Dest%" MD "%Dest%"

FOR /R "%Source%" %%A IN (*.jpg) DO FINDSTR -i "%%~nxA" "%FileNameList%" && (
	IF NOT EXIST "%Dest%%%~pA" MD "%Dest%%%~pA"
	copy /y "%%~fA" "%Dest%%%~pA%%~nA_Backup%%~xA"
)

PAUSE

Open in new window

0
 
LVL 10

Expert Comment

by:ReneGe
ID: 37041281
@fedexpert:
I posted my new script before you answered to Bill
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 37041306
This should handle renaming to just the first 6 characters.  It doesn't account for duplicates, not sure what you would want to do with those?

@echo off
setlocal EnableDelayedExpansion
set BaseDir=c:\fromdir
set DestDir=c:\todir
set ListFile=c:\temp\filelist.txt
for /F "usebackq tokens=*" %%F in ("%ListFile%") do (
  set Name=%%~nF
  copy "%BaseDir%\%%~F" "%DestDir%\!Name:0,6!.%%~xF"
)

Open in new window

~bp
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 37041316
Here you go

@ECHO OFF
SETLOCAL EnableDelayedExpansion

SET Source=C:\Source
SET Dest=C:\Destination
SET FileNameList=FileNameList.txt

IF NOT EXIST "%Dest%" MD "%Dest%"

FOR /R "%Source%" %%A IN (*.jpg) DO FINDSTR -i "%%~nxA" "%FileNameList%" && (
	IF NOT EXIST "%Dest%%%~pA" MD "%Dest%%%~pA"
	SET FileName=%%~nA
	SET FileName=!FileName:~0,6!
	copy /y "%%~fA" "%Dest%%%~pA!FileName!%%~xA"
)

PAUSE

Open in new window

0
 
LVL 10

Expert Comment

by:ReneGe
ID: 37041348
If you do not wish to keep path at destination,

Change line 14 from:
copy /y "%%~fA" "%Dest%%%~pA!FileName!%%~xA"
TO:
copy /y "%%~fA" "%Dest%\!FileName!%%~xA"
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 37041349
Rene's post brings up an interesting question, my posts have assumed the filenames in the TXT file include the extension, is that true?

~bp
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 37041391
In the question, it is stated JPG files. So I assume extension is not an issue.

However, in 37041214, poster confirmed script to be working. Therefore, the extension must be in the file list, text file.

Cheers,
Rene
0
 

Author Comment

by:fedexpert
ID: 37041414
Correct, the .jpg is within the text file.
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 37041439
Okay, my approach should be okay then.

~bp
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37041469
Thank you bill. I stand to be corrected. USEBACKQ is required when using double quotes around the filespec...

@echo off
set source=c:\source
set destination=c:\destination
set files=%source%\file.txt

for /f "tokens=* usebackq" %%a in ("%files%") do (
   copy /y "%source%\%%a" "%destination%" >nul
)

Open in new window


It just gets better and better...
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37041480
Ah! Just read the renaming thing... to do with 6 characters blah, blah, blah...
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37041507
I'm sure this works fine....

Don't forget to edit lines 4, 5 and 6 to point to your own files/locations.
@echo off
setlocal enabledelayedexpansion

set source=c:\source
set destination=c:\destination
set files=%source%\file.txt

for /f "tokens=* usebackq" %%a in ("%files%") do (
   set filename=%%~na
   copy /y "%source%\!filename!%%~xa" "%destination%" >nul
)

Open in new window

0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37041518
Oops! Pressed ENTER accidentally.... This is what you need!

@echo off
setlocal enabledelayedexpansion

set source=c:\source
set destination=c:\destination
set files=%source%\file.txt

for /f "tokens=* usebackq" %%a in ("%files%") do (
   set filename=%%~na
   copy /y "%source%\!filename:~0,6!%%~xa" "%destination%" >nul
)
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37041576
ReneGe...

Testing for, and creating the destination folder is a nice touch (I like seeing validation).

Also, testing for the existence of the textfile such as:

   if not exist "%source%\filelist.txt" (
      echo Error msg !!
      exit /b
   )


0
 
LVL 10

Expert Comment

by:ReneGe
ID: 37041652
Good points Paul!
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37041746
Blimey away for a bit sorting out stuff for tomorrow and thought it was in the bag (maybe divided by three) and all hell breaks loose!
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 37041769
@Steve: Sounds like you are having fun :)
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37041817
blowing up baloons, banners, chucking toys in piles....
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 37041836
Lucky you!!
0
 
LVL 11

Accepted Solution

by:
paultomasi earned 504 total points
ID: 37041837
Dear, dear, dear.... what a plonker... (tonight I'm NOT testing my code and therefore errors are creeping in left, right and centre!)

My code SHOULD HAVE read as follows (see renaming of files):


@echo off
setlocal enabledelayedexpansion

set source=c:\source
set destination=c:\destination
set files=%source%\file.txt

for /f "tokens=* usebackq" %%a in ("%files%") do (
   set filename=%%~na
   copy /y "%source%\%%a" "%destination%\!filename:~0,6!%%~xa" >nul
)
0
 

Author Comment

by:fedexpert
ID: 37041882
@ReneGe - Okay, this might be user error, but only one file is getting copied with the filename !FileName!.jpg.   Also, I made the suggested change to line 14 (RE: ID: 37041348
)   Is there something causing it to slow down because it took a REALLY long time to copy the one file?  
Its odd because @dragon-it code snippet copied the files relatively quickly considering the number of files.

@PaultoMasi/@BillPrew - still not able to get the file to run.

I have posted the code snippets in the attachment.

Thanks All, I am really appreciating your efforts
@ReneGe - 
@ECHO OFF

SET Source=E:\Cornerstone\Dallas
SET Dest=E:\Cornerstone\Upload
SET FileNameList=Files.txt

IF NOT EXIST "%Dest%" MD "%Dest%"
FOR /R "%Source%" %%A IN (*.jpg) DO FINDSTR -i "%%~nxA" "%FileNameList%" && (
	IF NOT EXIST "%Dest%%%~pA" MD "%Dest%%%~pA"
	SET FileName=%%~nA
	SET FileName=!FileName:~0,6!
	copy /y "%%~fA" "%Dest%\!FileName!%%~xA"
)

PAUSE
_________________________________________________________

@BillPrew
@echo off
setlocal EnableDelayedExpansion
set BaseDir=e:\cornerstone\dallas
set DestDir=e:\cornerstone\upload
set ListFile=e:\cornerstone\dallas\files.txt
for /F "usebackq tokens=*" %%F in ("%ListFile%") do (
  set Name=%%~nF
  copy "%BaseDir%\%%~F" "%DestDir%\!Name:0,6!.%%~xF"
)

Open in new window

0
 
LVL 10

Expert Comment

by:ReneGe
ID: 37041886
@fedexpert
You specified "The text file does not have the path, just the file name. "

Is it ok to assume that your files have a path other than the root dir of the source folder, and could also be in sub-direcrories?
0
 

Author Comment

by:fedexpert
ID: 37041904
I have a folder called Cornerstone. WIthin this folder, I have an Upload folder which is the destination folder for the photos....and I have a Dallas folder where the folders are stored.

The files.txt  lists the photos as


12345678.jpg
87654321.jpg

and so on.   Does that make sense?  Or did I make it worse?
0
 

Author Comment

by:fedexpert
ID: 37041907
*** I meant to say a Dallas folder where the photos are stored.  Sorry.
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37041923
bp... Dear, dear, dear... It's happening to you too...

A couple of errors (http:#37041306) in your line:

   copy "%BaseDir%\%%~F" "%DestDir%\!Name:0,6!.%%~xF"

which should read:

   copy "%BaseDir%\%%~F" "%DestDir%\!Name:~0,6!%%~xF"

Appears you missed a tilde (~) character (it happens to the best of us) and added one period (.) too many!


Also, I don't think there's a difference between:

   copy "%BaseDir%\%%~F"

and:

   copy "%BaseDir%\%%F"

in the current context.

Otherwise, you're good to go!
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37041965
In your code snippet above you have cut out the

"SETLOCAL EnableDelayedExpansion"

from reneGe's script which is why I imagine it is copying all the files over the top of the same file (which is why it seems slow for one file)

Steve
0
 

Author Closing Comment

by:fedexpert
ID: 37041970
@paultoMasi,  I just saw your last post.  Perfect. Thanks so much!

@dragonit, Renege - I also have a separate need for you posts as well, so points awarded. Thanks.

Thanks to all!  I truly appreciate your effort.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37042001
OK my offering then is:

@echo off
SETLOCAL EnableDelayedExpansion
set base=c:\cornerstone
cd /d "%base%"
for /f "delims=" %%a in ('type %base%\files.txt') do (
  set name=%%~na
  xcopy /D "%base%\dallas\!name:~0,6!%%~xa" "%base%\upload"
)

Steve
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37042056
Posting late here (having spent time setting up a mock environment etc...)

Just tested this and it works 100% !!


@echo off
setlocal enabledelayedexpansion

set files=e:\Cornerstone\Dallas\files.txt
set source=e:\Cornerstone\Dallas
set destination=e:\Cornerstone\Upload

for /f "tokens=* usebackq" %%a in ("%files%") do (
   set filename=%%~na
   copy /y "%source%\%%a" "%destination%\!filename:~0,6!%%~xa" >nul
)
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37042077
Oops! Didn't realise the question is already closed!...

Wish I had tested sooner...

Would have saved a lot of time in the long run!

Thanks a bunch for accepting my solution...

Thank you all - it was a joint effort!
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 37042144
That one was fun!
Very cool collaboration.

Thanks for the points!

Cheers,
Rene
0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question