Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

How to? Append files in multiple subfolders using copy?

Oct
364
17
Is it possible to append text files in different subfolders into a single file using copy without using a detailed looping approach?

Basically, I have this setup:

\2018\Sep
..\01
..\02
(etc ...)

About 20 of the subfolders contain a file with a standard name: PMT_2018-09-DD.txt where DD is the day.

I want to combine those into a single file.

I tried copy /s {pattern} F:\temp\target.txt but that copied the whole directory structure, etc., rather than appending.
 
Wow; you may have found an actual use for GLOBAL. Those are rare....

(Remember that you need to change to the top-level directory before using GLOBAL, e.g. with PUSHD.)
 
This worked.
Code:
v:\> type 1.txt
1

v:\> type rtt\2.txt
2

v:\> copy /b 1.txt+rtt\2.txt 3.txt
V:\1.txt => V:\3.txt
V:\rtt\2.txt =>> V:\3.txt
     2 files copied

v:\> type 3.txt
1
2

So did this.
Code:
v:\> type 1.txt
1

v:\> type rtt\2.txt
2

v:\> copy /b 1.txt+rtt\2.txt
V:\rtt\2.txt =>> V:\1.txt
     1 file copied

v:\> type 1.txt
1
2
 
GLOBAL together with file concatenation should work. Here's a very simple example. It seems that GLOBAL would benefit from an option to process only proper subdirectories (that is, to skip the top, current directory).

Code:
v:\sep> tree /f v:\sep

V:\sep
├──1
│  └  1.log
├──2
│  └  2.log
├──3
│  └  3.log
├──4
│  └  4.log
└──5
   └  5.log

v:\sep> do i=1 to 5 ( dir /m /k %i\%i.log )
2018-11-19  13:30               3  1.log
2018-11-19  13:30               3  2.log
2018-11-19  13:30               3  3.log
2018-11-19  13:30               3  4.log
2018-11-19  13:30               3  5.log

v:\sep> touch /c sep.log & global /iq if %_cwd ne v:\sep copy /b v:\sep\sep.log+*.log v:\sep\sep.log
2018-11-19 13:46:13.668  V:\sep\sep.log
V:\sep\1\1.log =>> V:\sep\sep.log
     1 file copied
V:\sep\2\2.log =>> V:\sep\sep.log
     1 file copied
V:\sep\3\3.log =>> V:\sep\sep.log
     1 file copied
V:\sep\4\4.log =>> V:\sep\sep.log
     1 file copied
V:\sep\5\5.log =>> V:\sep\sep.log
     1 file copied

v:\sep> dir /m /k /h
2018-11-19  13:30         <DIR>    1
2018-11-19  13:30         <DIR>    2
2018-11-19  13:30         <DIR>    3
2018-11-19  13:30         <DIR>    4
2018-11-19  13:30         <DIR>    5
2018-11-19  13:46              15  sep.log
 
FWIW,
Code:
do subdir in /a:d * ...
can be made to act like a (1-deep) GLOBAL and you don't have to worry about the top level.
 
FWIW,
Code:
do subdir in /a:d * ...
can be made to act like a (1-deep) GLOBAL and you don't have to worry about the top level.
Oh, that's much nicer than GLOBAL. And you can add a /S if you need to recurse deeper.
 
Yes, but Rick wanted to do it "without using a detailed looping approach ". Even GLOBAL is a "loop" but the missing details can make it hard to use.
 
Thanks! If forgot that the "names" of the files to be concatenated can include paths. This works:

copy .\*\PMT_20??-??-??.txt "F:\Temp\All_Pmts.txt"
 
Back
Top
[FOX] Ultimate Translator
Translate