3

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"
    )
)
  • Do you actually need/want to recurse into subdirs (as the command shown would do)? – wmz Apr 12 '15 at 8:01
  • @wmz yeah ..And i want the process to be done in the order of date modified from oldest to new . – CodeIt Apr 12 '15 at 10:55
  • that's difficult in 'pure' batch (because of how cmd [mis]treats dates). Would you accept powershell solution? – wmz Apr 12 '15 at 11:28
  • @wmz I'll accept only requirement is i should get everything done . – CodeIt Apr 13 '15 at 3:45
1
@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.

  • Please see my modified question. – CodeIt Apr 13 '15 at 13:06
  • @manutd, answer updated. – MC ND Apr 13 '15 at 14:10
  • @MC_ND I only used the %%a for writing the date.But the problem is that it is displaying it in yyyy/mm/dd format i want it in dd/mm/yyyy format . How should i do this. I have this command with me : FOR /f %%d IN ("%%~tf") DO echo Date : %%d and this successfully prints date in dd-mm-yyyy format but the code doesn't work with the script you have provided . – CodeIt Apr 14 '15 at 5:55
  • 1
    @manutd, change your echo(%%a into for /f "tokens=1-3 delims=/" %%d in ("%%a") do echo %%f/%%e/%%d, that is, tokenize the data using the slash as delimiter and output the elements in reversed order – MC ND Apr 14 '15 at 6:12
1

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.


  • Can you please modify the Ps one liner to add two lines of text on top of each file in the concatenated file.One for adding the file name without .txt extension and other for adding the date modified or last written date to the concatenated file. ex : File name : abcd //On the next line Date : dd/mm/yyyy and then the contents of the file . – CodeIt Apr 13 '15 at 13:11
  • @manutd Changed as per your comment – wmz Apr 13 '15 at 14:44
1
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.