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

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

W7 X-64 W8.1 X-64 Opensuse 13.1 W2003 Server
 
 

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

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

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

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

Windows 7 Ultimate x64
 
 

Happy to have helped.
My System SpecsSystem Spec
Reply

 Using DOs commands to merge txt files by date




Thread Tools



Similar help and support threads for2: Using DOs commands to merge txt files by date
Thread Forum
Batch Files - Create a Menu to Execute Commands Tutorials
MKV and MP4 Files - Merge into a Single File Tutorials
Merge PDF files? Software
How do I delete files from a certain date from a certain folder? General Discussion
Solved Merge two PDF files into one Software
Missing MERGE for .reg Files 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:53 PM.
Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App
  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33