Copy multiple files into a single file - xcopy?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I want to copy multiple text files into a single text file without having to
have all of the text files in one folder. The problem is that many of the
text files have the same name, so I want them to remain in separate
sub-folders. If the text files are all in the same folder, I know I can just
use the DOS command: copy *.txt file.txt.

I tried using xcopy *.txt C:\file.txt /s, hoping that all the text files in
the subfolders of my working directory would all get copied into file
C:\file.txt. But instead, it just overwrote file.txt a couple of thousand
times rather than combining all the files into one big text file.

Any advice?
 
Direct quote from Windows Help for Xcopy.

"Appending files
To append files, specify a single file for destination, but multiple
files for source (that is, by using wildcards or file1+file2+file3 format)."

My interpretation of past experience.
You need at least one + in there as I recall. Also you will want the
destination NOT in the same path as the source files.
 
JornyO said:
I want to copy multiple text files into a single text file without having to
have all of the text files in one folder. The problem is that many of the
text files have the same name, so I want them to remain in separate
sub-folders. If the text files are all in the same folder, I know I can just
use the DOS command: copy *.txt file.txt.

I tried using xcopy *.txt C:\file.txt /s, hoping that all the text files in
the subfolders of my working directory would all get copied into file
C:\file.txt. But instead, it just overwrote file.txt a couple of thousand
times rather than combining all the files into one big text file.

Any advice?

I think an example would go a long way towards describing
your exact requirements accurately.
 
What I wrote earlier basically was an example of what I am trying to do. In
the folder C:\Data there are multiple sub-folders (\010101, \010102, \010103,
etc...) that each contain several text files. I can't copy all of the text
files from these subfolders into a single folder (in order to use "copy" in
command prompt) because there are files in the different subfolders with the
same name. What I was trying to do with the xcopy command was copy all of
the contents of the .txt files in the subfolders into a single .txt file
located in C:\.

The command I tried was: xcopy *.txt C:\file.txt /s

This command did in fact copy each text file from the subfolders, but rather
than appending them all into the same file, it just overwrote C:\file.txt
each time it copied one of the text files from one of the subfolders. For
some reason, using the simple command "copy *.txt file.txt" works fine if all
of the source text files are in the same folder. But as I said, I want to
get text files from the subfolders and have them all combined into a single
text file. Thanks for your time so far! The help is greatly appreciated.
 
Try this batch file:

@echo off
set HomeFolder=c:\temp

Echo Snapshot taken on %date% at %time:~0,5% > c:\Collect.txt
call :Sub %HomeFolder%
for /F "tokens=*" %%* in ('dir /s /b /ad') do call :Sub %%*
notepad c:\Collect.txt
goto :eof

:Sub
pushd "%*"
if exist *.txt (
echo. >> c:\Collect.txt
echo Folder=%* >> C:\Collect.txt
echo ==================== >> c:\Collect.txt
for %%b in (*.txt) do (
echo %%b >> c:\Collect.txt
type "%%b" >> c:\Collect.txt
)
)
popd

Make sure to copy & paste it rather than retyping it.
 
I tried the code you suggested by copying and pasting it into a text file and
then changing the extension to .bat. I tried running it twice, once by
double-clicking on the Batch file icon, and once from Command Prompt. Both
times I got the same result: a 1KB file called Collect.txt in C:\ with a
single line of text that says: "Snapshot taken on 03/15/2007 at 9:00" That
batch file was in the parent folder whose subfolders contain the text files I
want copied into a single file.

Did I miss something? Perhaps my limited knowledge of coding and scripting
is making blind to something that ought to be obvious.



:

@echo off
set HomeFolder=c:\temp

Echo Snapshot taken on %date% at %time:~0,5% > c:\Collect.txt
call :Sub %HomeFolder%
for /F "tokens=*" %%* in ('dir /s /b /ad') do call :Sub %%*
notepad c:\Collect.txt
goto :eof

:Sub
pushd "%*"
if exist *.txt (
echo. >> c:\Collect.txt
echo Folder=%* >> C:\Collect.txt
echo ==================== >> c:\Collect.txt
for %%b in (*.txt) do (
echo %%b >> c:\Collect.txt
type "%%b" >> c:\Collect.txt
)
)
popd
 
JornyO said:
I tried the code you suggested by copying and pasting it into a text file and
then changing the extension to .bat. I tried running it twice, once by
double-clicking on the Batch file icon, and once from Command Prompt. Both
times I got the same result: a 1KB file called Collect.txt in C:\ with a
single line of text that says: "Snapshot taken on 03/15/2007 at 9:00" That
batch file was in the parent folder whose subfolders contain the text files I
want copied into a single file.

Did I miss something? Perhaps my limited knowledge of coding and scripting
is making blind to something that ought to be obvious.



:

@echo off
set HomeFolder=c:\temp

Echo Snapshot taken on %date% at %time:~0,5% > c:\Collect.txt
call :Sub %HomeFolder%
for /F "tokens=*" %%* in ('dir /s /b /ad') do call :Sub %%*
notepad c:\Collect.txt
goto :eof

:Sub
pushd "%*"
if exist *.txt (
echo. >> c:\Collect.txt
echo Folder=%* >> C:\Collect.txt
echo ==================== >> c:\Collect.txt
for %%b in (*.txt) do (
echo %%b >> c:\Collect.txt
type "%%b" >> c:\Collect.txt
)
)
popd

While you're waiting for Pegasus to explain the esoterica of
command-line programming, try editing the second line of your batch file
to read:

set HomeFolder=c:\Data

(assuming that C:\Data is parent folder whose subfolders contain the
text files you want copied into a single file; otherwise, use the
correct folder name)
 
What I wrote earlier basically was an example of what I am trying to do. In
the folder C:\Data there are multiple sub-folders (\010101, \010102, \010103,
etc...) that each contain several text files. I can't copy all of the text
files from these subfolders into a single folder (in order to use "copy" in
command prompt) because there are files in the different subfolders with the
same name. What I was trying to do with the xcopy command was copy all of
the contents of the .txt files in the subfolders into a single .txt file
located in C:\.

The command I tried was: xcopy *.txt C:\file.txt /s

This command did in fact copy each text file from the subfolders, but rather
than appending them all into the same file, it just overwrote C:\file.txt
each time it copied one of the text files from one of the subfolders. For
some reason, using the simple command "copy *.txt file.txt" works fine if all
of the source text files are in the same folder. But as I said, I want to
get text files from the subfolders and have them all combined into a single
text file. Thanks for your time so far! The help is greatly appreciated.

Check out the following utility -

http://bluefive.pair.com/txtcollector.htm
 
I cannot offer any further advice unless you post ***your***
version of the batch file.
 
Back
Top