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

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.
fedexpertAsked:
Who is Participating?
 
Paul TomasiCommented:
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
 
effxCommented:
do you have the option of using VBS?
0
 
fedexpertAuthor Commented:
No, that is not available to me.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Paul TomasiCommented:
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
 
ReneGeCommented:
@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
 
Steve KnightIT ConsultancyCommented:
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
 
Steve KnightIT ConsultancyCommented:
Three at exactly the same time... that must be a record... Billprew / billDL you disappoint me on not making it 5 :-)

Steve
0
 
ReneGeCommented:
Coool!
0
 
Paul TomasiCommented:
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
 
ReneGeCommented:
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
 
Bill PrewCommented:
@Steve

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

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

~bp
0
 
Bill PrewCommented:
@Paul

Won't you need usebackq ?

~bp
0
 
Bill PrewCommented:
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
 
fedexpertAuthor Commented:
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
 
Bill PrewCommented:
How would you want them renamed?

~bp
0
 
fedexpertAuthor Commented:
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
 
ReneGeCommented:
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
 
ReneGeCommented:
@fedexpert:
I posted my new script before you answered to Bill
0
 
Bill PrewCommented:
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
 
ReneGeCommented:
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
 
ReneGeCommented:
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
 
Bill PrewCommented:
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
 
ReneGeCommented:
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
 
fedexpertAuthor Commented:
Correct, the .jpg is within the text file.
0
 
Bill PrewCommented:
Okay, my approach should be okay then.

~bp
0
 
Paul TomasiCommented:
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
 
Paul TomasiCommented:
Ah! Just read the renaming thing... to do with 6 characters blah, blah, blah...
0
 
Paul TomasiCommented:
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
 
Paul TomasiCommented:
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
 
Paul TomasiCommented:
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
 
ReneGeCommented:
Good points Paul!
0
 
Steve KnightIT ConsultancyCommented:
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
 
ReneGeCommented:
@Steve: Sounds like you are having fun :)
0
 
Steve KnightIT ConsultancyCommented:
blowing up baloons, banners, chucking toys in piles....
0
 
ReneGeCommented:
Lucky you!!
0
 
fedexpertAuthor Commented:
@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
 
ReneGeCommented:
@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
 
fedexpertAuthor Commented:
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
 
fedexpertAuthor Commented:
*** I meant to say a Dallas folder where the photos are stored.  Sorry.
0
 
Paul TomasiCommented:
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
 
Steve KnightIT ConsultancyCommented:
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
 
fedexpertAuthor Commented:
@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
 
Steve KnightIT ConsultancyCommented:
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
 
Paul TomasiCommented:
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
 
Paul TomasiCommented:
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
 
ReneGeCommented:
That one was fun!
Very cool collaboration.

Thanks for the points!

Cheers,
Rene
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.