Solved Using DOs commands to merge txt files by date

nycblkboy

New member
Local time
8:19 AM
Messages
6
I have a bunch of txt files that i want to merge into one file. But instead of A-Z order I want to do it by date, oldest first.

ie:

files/date:
1.txt 1/1/2001
2.txt 1/1/2000
3.txt 1/1/2002
etc

I want the combined file contents to be the contents of 1.txt+3.txt+2.txt,etc This is as far as i've gotten:
dir /B /o : D > %1
copy %1 newfile4.txt

I just need copy to take the variable and it should work. Why is this so hard though? I can't even find a windows 7 program to do this exact thing. I've been working on this for hours please help :(
 

My Computer My Computer

At a glance

Windows 7 PRo 64-bit
OS
Windows 7 PRo 64-bit
I have a bunch of txt files that i want to merge into one file. But instead of A-Z order I want to do it by date, oldest first.

ie:

files/date:
1.txt 1/1/2001
2.txt 1/1/2000
3.txt 1/1/2002
etc

I want the combined file contents to be the contents of 1.txt+3.txt+2.txt,etc This is as far as i've gotten:
dir /B /o : D > %1
copy %1 newfile4.txt

I just need copy to take the variable and it should work. Why is this so hard though? I can't even find a windows 7 program to do this exact thing. I've been working on this for hours please help :(

Hi there
Maybe the Youngsters are so used to a GUI that command line stuff has been forgotten

This should do what you want. !!!!!

TYPE B.TXT>>A.TXT

Cheers
jimbo
 

My Computer My Computer

At a glance

Linux CENTOS 7 / various Windows OS'es and se...Intel i7 Intel i58GB, 16GBOn Motherboard
Computer type
PC/Desktop
Computer Manufacturer/Model Number
Custom built, several laptops HP/ASUS
OS
Linux CENTOS 7 / various Windows OS'es and servers
CPU
Intel i7 Intel i5
Memory
8GB, 16GB
Graphics Card(s)
On Motherboard
Sound Card
Realtek HD audio
Monitor(s) Displays
Apple Cinema display, Samsung LCD
Screen Resolution
1920 X 1080
Hard Drives
4 X 1TB SATA
Mouse
Toshiba wireless laser
Internet Speed
> 20MB up
I have a bunch of txt files that i want to merge into one file. But instead of A-Z order I want to do it by date, oldest first.

ie:

files/date:
1.txt 1/1/2001
2.txt 1/1/2000
3.txt 1/1/2002
etc

I want the combined file contents to be the contents of 1.txt+3.txt+2.txt,etc This is as far as i've gotten:
dir /B /o : D > %1
copy %1 newfile4.txt

I just need copy to take the variable and it should work. Why is this so hard though? I can't even find a windows 7 program to do this exact thing. I've been working on this for hours please help :(

Welcome to the windows 7 forums nycblkboy

The easiest method that I can recommend is to do this.

copy 1.txt+3.txt+2.txt 4.txt

if the files were binary files, then the /b option would be required. but text files only copy
up to the controlZ which is the EOF for text files.

rich
 

My Computer My Computer

At a glance

Windows 7 Pro x64 SP1Intel Core I716 GigsNVIDIA GeForce GTX 670M
Computer type
Laptop
Computer Manufacturer/Model Number
Toshiba Laptop Qosimo X870
OS
Windows 7 Pro x64 SP1
CPU
Intel Core I7
Motherboard
Toshiba Qosmio
Memory
16 Gigs
Graphics Card(s)
NVIDIA GeForce GTX 670M
Monitor(s) Displays
17.7" laptop
Screen Resolution
1600 x 900
Hard Drives
256 Gig SanDisk SSD for C
256 Gig Intel SSD for D
Internet Speed
50/25 FIOS
Antivirus
Vipre (all you can eat for 10 machines)
Browser
IE and FF
Other Info
I have dos 6.22, wfwg 3.11, win98, 2000 and xp VHD's available for testing. MS's Virtual PC works great.
dir /B /o : D > %1
copy %1 newfile4.txt
The reason this fails is because there is no + signs and the dir command lists each file on it's own line and like richnrockville said the syntax for the copy cmd needs to be "copy 1.txt+3.txt+2.txt newfile4.txt".

This will work for you (also works with text files that have spaces in their name)
Code:
for /f "tokens=*" %i in ('dir /b /o:d C:\FolderName') do type "C:\FolderName\%i">> new.txt && echo.>> new.txt
Replace C:\FolderName with the path to the folder that contains the text files.

You need to run this from anywhere in cmd.exe just not in C:\FolderName or you'll end up doubling up because new.txt will also be appended last to the end of itself, unless you specify a full path for new.txt outside of C:\FolderName.

"&& echo.>> new.txt" will start the next text file on a new line in case the preceeding one doesn't end with a blank line. You may want to add a 2nd one of these if you want a blank line between the files but if one file already ends with a blank line then you'll have 2 blank lines in the new file.

No need to use a batch file but if you wanted to then you need to replace the 2 occurrences of %i with %%i

This should do what you want. !!!!!

TYPE B.TXT>>A.TXT
The easiest method that I can recommend is to do this.

copy 1.txt+3.txt+2.txt 4.txt
Both of these are correct but they need to be ordered oldest first so that is a hassle to type this up if there's 30 or 40 files.

EDIT: By default "dir /o:d" sorts by "Last Written" (Modified) but you can override this with the /t switch if you want to order by Created or Accessed.
 

My Computer My Computer

At a glance

Windows 7 Ultimate x64Intel Pentium Dual Core E5200 2.5GHz (3.77GHz...Corsair 4GB DDR2 (4x1GB CM2X1024-6400C4)Palit GeForce GTS 250 (1024MB)
Computer Manufacturer/Model Number
Self built
OS
Windows 7 Ultimate x64
CPU
Intel Pentium Dual Core E5200 2.5GHz (3.77GHz OC)
Motherboard
Asus P5Q-E
Memory
Corsair 4GB DDR2 (4x1GB CM2X1024-6400C4)
Graphics Card(s)
Palit GeForce GTS 250 (1024MB)
Sound Card
On Board (ADI AD2000B 8ch HD)
Monitor(s) Displays
Samsung 32in LCD TV
Screen Resolution
1360x768
Hard Drives
2 x 1TB Samsung 103SJ (Raid0)
2 x External 500GB Samsung 502IJ (NexStar 3 HD Enclosures)
PSU
550W Antec Neo HE 550
Case
Antec P180
Cooling
Xigmatex Red Scorpion CPU Cooler. 3x120mm Fans
Keyboard
Logitech MX5000 Laser (Combo)
Mouse
Logitech MX5000 Laser (Combo)
Internet Speed
ADSL2+ (avg 10 Mbps Down, 0.80 Mbps up)
Other Info
Gigabyte GN-WP01GS 54g Wireless Lan Card
dir /B /o : D > %1
copy %1 newfile4.txt
The reason this fails is because there is no + signs and the dir command lists each file on it's own line and like richnrockville said the syntax for the copy cmd needs to be "copy 1.txt+3.txt+2.txt newfile4.txt".

This will work for you (also works with text files that have spaces in their name)
Code:
for /f "tokens=*" %i in ('dir /b /o:d C:\FolderName') do type "C:\FolderName\%i">> new.txt && echo.>> new.txt
Replace C:\FolderName with the path to the folder that contains the text files.

You need to run this from anywhere in cmd.exe just not in C:\FolderName or you'll end up doubling up because new.txt will also be appended last to the end of itself, unless you specify a full path for new.txt outside of C:\FolderName.

"&& echo.>> new.txt" will start the next text file on a new line in case the preceeding one doesn't end with a blank line. You may want to add a 2nd one of these if you want a blank line between the files but if one file already ends with a blank line then you'll have 2 blank lines in the new file.

No need to use a batch file but if you wanted to then you need to replace the 2 occurrences of %i with %%i

This should do what you want. !!!!!

TYPE B.TXT>>A.TXT
The easiest method that I can recommend is to do this.

copy 1.txt+3.txt+2.txt 4.txt
Both of these are correct but they need to be ordered oldest first so that is a hassle to type this up if there's 30 or 40 files.

EDIT: By default "dir /o:d" sorts by "Last Written" (Modified) but you can override this with the /t switch if you want to order by Created or Accessed.

Thank you Duzzy. I knew thee ways that the other 2 posters said. I don't think they understood I didn't want to do this manually. Thanks again you are a God!
 

My Computer My Computer

At a glance

Windows 7 PRo 64-bit
OS
Windows 7 PRo 64-bit
Happy to have helped.
 

My Computer My Computer

At a glance

Windows 7 Ultimate x64Intel Pentium Dual Core E5200 2.5GHz (3.77GHz...Corsair 4GB DDR2 (4x1GB CM2X1024-6400C4)Palit GeForce GTS 250 (1024MB)
Computer Manufacturer/Model Number
Self built
OS
Windows 7 Ultimate x64
CPU
Intel Pentium Dual Core E5200 2.5GHz (3.77GHz OC)
Motherboard
Asus P5Q-E
Memory
Corsair 4GB DDR2 (4x1GB CM2X1024-6400C4)
Graphics Card(s)
Palit GeForce GTS 250 (1024MB)
Sound Card
On Board (ADI AD2000B 8ch HD)
Monitor(s) Displays
Samsung 32in LCD TV
Screen Resolution
1360x768
Hard Drives
2 x 1TB Samsung 103SJ (Raid0)
2 x External 500GB Samsung 502IJ (NexStar 3 HD Enclosures)
PSU
550W Antec Neo HE 550
Case
Antec P180
Cooling
Xigmatex Red Scorpion CPU Cooler. 3x120mm Fans
Keyboard
Logitech MX5000 Laser (Combo)
Mouse
Logitech MX5000 Laser (Combo)
Internet Speed
ADSL2+ (avg 10 Mbps Down, 0.80 Mbps up)
Other Info
Gigabyte GN-WP01GS 54g Wireless Lan Card
Back
Top