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

###### Who is Participating?
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.

CEOCommented:
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
0
Commented:
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%"
)


»bp
1

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.

Software testerAuthor Commented:
Bill - spot on and appreciated.
0
Commented:
Welcome Terry, glad that was useful.

»bp
0
###### 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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.