Windows 7 Forums

Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: Using DOs commands to merge txt files by date

30 Apr 2012   #1
nycblkboy

Windows 7 PRo 64-bit
 
 
Using DOs commands to merge txt files by date

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 System SpecsSystem Spec
.
30 Apr 2012   #2
jimbo45

Linux CENTOS 7 / various Windows OS'es and servers
 
 

Quote   Quote: Originally Posted by nycblkboy View Post
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 System SpecsSystem Spec
30 Apr 2012   #3
richnrockville

Windows 7 Pro x64 SP1
 
 

Quote   Quote: Originally Posted by nycblkboy View Post
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 System SpecsSystem Spec
.

30 Apr 2012   #4
Duzzy

Windows 7 Ultimate x64
 
 

Quote   Quote: Originally Posted by nycblkboy View Post
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

Quote   Quote: Originally Posted by jimbo45 View Post
This should do what you want. !!!!!

TYPE B.TXT>>A.TXT
Quote   Quote: Originally Posted by richnrockville View Post
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 System SpecsSystem Spec
30 Apr 2012   #5
nycblkboy

Windows 7 PRo 64-bit
 
 

Quote   Quote: Originally Posted by Duzzy View Post
Quote   Quote: Originally Posted by nycblkboy View Post
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

Quote   Quote: Originally Posted by jimbo45 View Post
This should do what you want. !!!!!

TYPE B.TXT>>A.TXT
Quote   Quote: Originally Posted by richnrockville View Post
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 System SpecsSystem Spec
01 May 2012   #6
Duzzy

Windows 7 Ultimate x64
 
 

Happy to have helped.
My System SpecsSystem Spec
Reply

 Using DOs commands to merge txt files by date




Thread Tools Search this Thread
Search this Thread:

Advanced Search




Similar help and support threads
Thread Forum
Batch Files - Create a Menu to Execute Commands
How to Use a Batch File to Create a Command Prompt Menu to Execute Commands This tutorial shows the process one goes through when creating a batch file that opens a command prompt menu allowing you to launch commands, programs and more. I will only be executing programs in this tutorial,...
Tutorials
MKV and MP4 Files - Merge into a Single File
How to Combine Multiple MKV or MP4 Segments into 1 File If you have multiple .MKV or MP4 files that you would like to to merge together, forming a single MKV file, without recoding, this tutorial will show you how. You will need MKV merge for this task. For MeGUI Users:
Tutorials
Making files in subfolders sort by "Date" or "Date Taken" by default?
(Running Windows 7 Professional x64 SP1) I'm a professional photographer and have recently begun shooting with two cameras simultaneously at events. They use different file names to avoid duplicate names/confusion, i.e. IMG_175000 and XMG_175000. Often times I'll take a photo with one camera and...
General Discussion
Merge PDF files?
Anyone know how to merge more than 2 PDF files into1 so I can send in emails? Thanks.
Software
Merge two PDF files into one
I'm hoping that someone would be able to tell me how to merge two PDF files into one. I'm currently using Adobe Reader. Is it even possible? If not, how could I make it possible? Thanks
Software
Missing MERGE for .reg Files
When I right-click a .reg file, Open rather than Merge appears in the popup list. What do I do to fix this problem? BTW, I'm trying to fix an empty Favorites problem as suggested in - Tom
General Discussion


Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd

All times are GMT -5. The time now is 01:46.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App