Batch file - write all text file contents and sort

I have a folder full of text files.
I need these text files names and contents written into a new text file sorted by text files date modified.

Please and thanks
terry gehlerSoftware testerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dovidfCEOCommented:
The batch file should contain the following

dir *.txt /on >out.txt  if you run it in the directory

You can also specify the directory

dir c:/<dir name>/*.txt /on >out.txt

Out. txt is the result file which you can name to anything you want
Bill PrewIT / Software Engineering ConsultantCommented:
This should do what you want, adjust the folder and file name at the top.  Make sure you don't place the merged file in the same folder as the individual files or you could get into a nasty loop as it keeps processing the output file.

@echo off
setlocal

set "BaseDir=b:\ee\ee29068879\files"
set "MergeFile=b:\ee\ee29068879\merge.txt"

if exist "%MergeFile%" del "%MergeFile%"

for /f "tokens=*" %%F in ('dir /b /od "%BaseDir%\*.*"') do (
    echo.========== %%F ==========>>"%MergeFile%"
    type "%BaseDir%\%%~F">>"%MergeFile%"
)

Open in new window


»bp

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
terry gehlerSoftware testerAuthor Commented:
Bill - spot on and appreciated.
Bill PrewIT / Software Engineering ConsultantCommented:
Welcome Terry, glad that was useful.


»bp
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.