Sorting list in alphabetical order?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
MrKnowItAllxx
Posts: 43
Joined: 20 Mar 2012 20:53

Sorting list in alphabetical order?

#1 Post by MrKnowItAllxx » 19 Apr 2012 17:40

I have a text file that I would like to be sorted (or just displayed) in alphabetical order

Currently I am using 'for /f "tokens=* delims=" %%a in (myfile.txt) do echo %%a'

I simply want the output alphabetical, any solution is fine

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Sorting list in alphabetical order?

#2 Post by foxidrive » 19 Apr 2012 18:41

Displayed:

Code: Select all

sort <file.txt



Redirected into another file:

Code: Select all

sort <file.txt >outfile.txt

MrKnowItAllxx
Posts: 43
Joined: 20 Mar 2012 20:53

Re: Sorting list in alphabetical order?

#3 Post by MrKnowItAllxx » 19 Apr 2012 20:10

Code: Select all

for /f "tokens=* delims=" %%a in ('sort <myfile.txt') do echo %%a

Output:

Code: Select all

< was unexpected at this time.
C:\Users\User\Desktop>


How could I accomplish this in this format?? The file is not organized in a way that would generate suitable output from 'sort <myfile.txt' so I was hoping I could embed this in a for loop as opposed to restructuring the whole file :?

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Sorting list in alphabetical order?

#4 Post by Aacini » 19 Apr 2012 22:28

You must escape the < redirection sign:

Code: Select all

for /f "tokens=* delims=" %%a in ('sort ^<myfile.txt') do echo %%a
Note that tokens=* is not required in this case:

Code: Select all

for /f "delims=" %%a in ('sort ^<myfile.txt') do echo %%a

MrKnowItAllxx
Posts: 43
Joined: 20 Mar 2012 20:53

Re: Sorting list in alphabetical order?

#5 Post by MrKnowItAllxx » 20 Apr 2012 20:14

Thank you very much Aacini and foxidrive :) Problem solved

Post Reply