Monday, August 24, 2015

windows - Command Line : Concatenate multiple files by date


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"
)
)

Answer



@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.


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...