Sorting Batch File Results

I have the following in a batch file.

for /D %%A IN (*) DO echo %%A>> output.txt

      

I want the results of this to be in alphabetical order, instead it appears to be ordered by date.

Can anyone suggest how this can be achieved?

+3


source to share


2 answers


You can use the dir command to sort your folders and echo them ...

for /f "delims=" %%a in ( 'dir /b /ad /oN') do echo %%a  >> output.txt

      

And if you only want directory echo



dir / b / ad / oN -> output.txt

will be sufficient...

On the command line help for

and help dir

you can use ...

+4


source


Just use the command sort

.

for /D %%A IN (*) DO echo %%A>> output.txt
sort output.txt > sorted_output.txt

      

you can find out more about this command by calling sort /?

or here .

As you can read with the command, you can use /O

to redirect the output to a file instead of stdout. And with that, you can avoid the need for two files:



for /D %%A IN (*) DO echo %%A>> output.txt
sort output.txt /O output.txt

      

EDIT: A good one-liner might be:

(for /D %%A IN (*) DO echo %%A) | sort > output.txt

      

It is also much faster as it writes the output once at the end.

+2


source







All Articles