Command Line : Concatenate multiple files by date

Posted on

Problem :

I have got a plenty of .txt files in a directory. The directory also contains .png and some .pdf files. I have successfully concatenated .txt files using this command :

@ECHO Off
SETLOCAL
for /r %%f in (*.txt) do (
echo.
type "%%f"
)>> output.doc

GOTO :EOF

The above command was obtained from one of the online sites. This command will concatenate .txt files in alphabetical order. But i don’t want it like that , i want it to be done by the date modified or created (modified is recommended).
I have got a hint that the line

for /r %%f in(*.txt) do ( 

must be modified in order to concatenate by date.I’m new to this command line or batch scripting so i don’t really know much about it. How should i do this .
Thanks everyone for the answers.

Sorry for not including this question in the first time.
I have got one more requirement based on another question that i asked previously(Question) i have got a batch file for concatenating .txt files and for adding two lines on top of every file (one for writing the file name without the extension and other for writing the date associated to the concatenated file).Can anyone please modify the below batch file to concatenate .txt files by the order of date-modified and to add the two lines on top of each file.

@echo off
SETLOCAL
for /r %%f in (*.txt) do (
echo File Name   : %%~nf 
FOR /f %%d IN ("%%~tf") DO echo Date        : %%d
echo.
type "%%f"
) >> output_text.doc

GOTO :EOF

Every problem solved final batch file :

@echo off
setlocal enableextensions disabledelayedexpansion

set "root=%cd%"

>"output.doc" (
    for /f "tokens=1,2,*" %%a in ('
        robocopy "%root%." "%root%." *.txt /l /s /is /ts /ndl /njh /njs /nc /ns 
        ^| sort 
    ') do (
     echo( %%~nc
     for /f "tokens=1-3 delims=/" %%d in ("%%a") do echo %%f/%%e/%%d
     type "%%~fc"
    )
)

Solution :

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "root=%cd%"

    >"output.doc" (
        for /f "tokens=2,*" %%a in ('
            robocopy "%root%." "%root%." *.txt /l /s /is /ts /ndl /njh /njs /nc /ns 
            ^| sort 
        ') do type "%%~fb"
    )

This will use robocopy to get the list of all the .txt files under the indicated root folder.

The list of files is generated including the last modified date of the files. robocopy prints the UTC time in yyyy/mm/dd hh:mm:ss format, so the list can be properly sorted.

The rest of the code is just a for /f command to process the final list, retrieving the file reference and typing it. The full command is redirected to the output file to avoid the open/close operation for each processed file.

edited as it is needed to include the file name and the timestamp in the final output …

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "root=%cd%"

    >"output.doc" (
        for /f "tokens=1,2,*" %%a in ('
            robocopy "%root%." "%root%." *.txt /l /s /is /ts /ndl /njh /njs /nc /ns 
            ^| sort 
        ') do (
            echo(%%~nc
            echo(%%a %%b [%%~tc]
            type "%%~fc"
        )
    )

The tokens clause has been changed to retrieve the timestamp from robocopy so we can include it in the output. %%a will hold the date, %%b the time and %%c the file name with full path.

For each file, it is first echoed the file name without extension (%%~nc), then the robocopy time stamp and, just to compare as the output of robocopy is a UTC time, the file timestamp (%%~tc). Then the file is typed.

As before, all the output is redirected to the final file.

Powershell (3 -see note) one liner:

gci *.txt -Recurse -File |sort -Property LastWriteTime |% {(gc $_) + "`n"} >output.doc

  • It first lists all matching files (gci)
  • then sorts them by modified date (sort)
  • then gets contents of file and appends a newline to each (gc)
  • and finally writes all to output file.

Note: Because it uses -File which was introduced in ps3. This can be rewritten in Ps2 syntax, but it’s more verbose.

Edit: as requested by OP: modify {(gc $_) + “`n”}
to read:

{"$($_.basename)`n$(get-date -u %d/%m/%Y $_.lastwritetime)`n$(gc $_)`n"}


(string interpolation used this time to show various options)

Side note: [possible] file encoding inconsistencies & problems not handled.


for /f "delims=:" %f in ('dir *.txt /O:D /T:W /B') do @echo %f

This command will use dir to sort by oldest to newest date (/O:D) using the last written date (/T:W) and bare format (/B) to just return the filenames. Setting the delims to : in the for command keeps it from trying to parse the filename based on spaces or commas since : is not allowed in a filename.

Leave a Reply

Your email address will not be published.