Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Coping multiple files (w/filenames in txt file)? 1

Status
Not open for further replies.

cpjust

Programmer
Sep 23, 2003
2,132
US
This really shouldn't be this hard, but Windows it making it a million times harder than it should be...

I have a list of files (with relative paths) in a text file named newfiles.txt and I want to copy all those files to another directory.
I created this batch file:
Code:
@ECHO OFF
SETLOCAL
SET dest=%1

:Begin
SHIFT
IF "%1"=="" GOTO End
XCOPY %1 %dest%\%1
GOTO Begin

:End
ENDLOCAL
but when I run it like this:
Code:
CopyFiles.bat C:\Temp < newfiles.txt
it only sees one command line argument "C:\Temp", so no files are copied.
Why isn't that working?

All I want to do is backup (either copy to another directory, zip or tar) the files listed in newfiles.txt.
Is there an easier way to do what I want?
 
How about something like:

Code:
for /f %%a in (newfiles.txt) DO XCOPY %%a ....

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks, that works almost flawlessly!
Now the only thing left is to find a way to create the directory structure and copy the file without prompting me. Currently if I use this script:
Code:
@ECHO OFF
SETLOCAL
IF "%2"=="" GOTO Error
SET dest=%1
SET file=%2

FOR /F %%a IN (%file%) DO XCOPY /E /I /T %%a %dest%\%%a
GOTO End

:Error
ECHO Syntax: %0 <destination> <input file>

:End
ENDLOCAL

it gives me this prompt for every file:
Code:
Does D:\Temp\SubDir\filename.h specify a file name
or directory name on the target
(F = file, D = directory)?

The input file looks something like this:
Code:
SubDir\filename.h
SubDir\SubDir1\filename.cpp
...

There must be some way to have XCOPY automatically choose the 'F' option?
 
Try removing the last instance of the file name in your case the last %%a variable. xcopy will then automatically just create the file if it is a file no prompting

Code:
FOR /F %%a IN (%file%) DO XCOPY /E /I /T %%a %dest%\




----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
That doesn't quite work. It creates the directory structure, but copies all files to the %dest% directory.
If I use 2 FOR loops like this, it does work though:
Code:
FOR /F %%a IN (%file%) DO XCOPY /T %%a %dest%
FOR /F %%a IN (%file%) DO COPY %%a %dest%\%%a
It doesn't work for files/directories with spaces in the names, but I don't have many of those anyways.
 
I see, Glad you got it working though.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top