Using DOs commands to merge txt files by date


  1. Posts : 6
    Windows 7 PRo 64-bit
       #1

    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 Computer


  2. Posts : 5,941
    Linux CENTOS 7 / various Windows OS'es and servers
       #2

    nycblkboy said:
    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


  3. Posts : 1,800
    Windows 7 Pro x64 SP1
       #3

    nycblkboy said:
    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


  4. Posts : 640
    Windows 7 Ultimate x64
       #4

    nycblkboy said:
    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

    jimbo45 said:
    This should do what you want. !!!!!

    TYPE B.TXT>>A.TXT
    richnrockville said:
    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


  5. Posts : 6
    Windows 7 PRo 64-bit
    Thread Starter
       #5

    Duzzy said:
    nycblkboy said:
    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

    jimbo45 said:
    This should do what you want. !!!!!

    TYPE B.TXT>>A.TXT
    richnrockville said:
    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


  6. Posts : 640
    Windows 7 Ultimate x64
       #6

    Happy to have helped.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About 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 06:04.
Find Us