How to sort numbers in a text file?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

How to sort numbers in a text file?

#1 Post by MKANET » 30 Apr 2012 10:04

I have a text file with contents such as (forefiles command output). Since forefiles doesnt have the ability to sort by size, I need a way to do this using a batch file script.

Code: Select all

"a:\AmericanDad-S07E15-LessMoneyMoProblems-14669961-0.mpg" 2986316918
"a:\AmericanDad-S07E16-TheKidneyStaysinthePicture-14751494-0.mpg" 2954978570
"a:\Californication-S05E12-HellAintaBadPlacetoBe-14751936-0.mpg" 4342208176
"a:\DoomsdayPreppers-S01E10-DisasterDoesntWait-15205666-0.mpg" 7171749364
"a:\DoomsdayPreppers-S01E10-DisasterDoesntWait-15205666-0.txt" 352


I tried using the sort command (where filez.txt is the file with the contents mentioned above); however, it's definitely not working correctly. Can someone please help?

Code: Select all

for /f "tokens=1,2 delims= " %%a in (filez.txt) do echo %%b >>fileza.txt
sort <fileza.txt >outfile.txt

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

Re: How to sort numbers in a text file?

#2 Post by foxidrive » 30 Apr 2012 10:29

MKANET wrote:Can someone please help?


Do you want to sort the lines according to the numbers at the end of the line? What number range can the numbers take?

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: How to sort numbers in a text file?

#3 Post by Squashman » 30 Apr 2012 10:42

I think I would go back to how you created the text file and create it in a more fixed text format because SORT can specify the sort position to start with.

Or put the numbers first in the text file with some spaces padded at the end or use the DIR command to sort the largest first when creating the text file.

Update: Ugh. Sort doesn't sort numerically. It sorts character by character.

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: How to sort numbers in a text file?

#4 Post by !k » 30 Apr 2012 11:44

Code: Select all

setlocal enableextensions
for /f "tokens=1,2" %%a in (filez.txt) do (
   set "size=............%%b"
   call set %%size:~-12%%=%%a
)
for /f "tokens=1,2 delims==" %%a in ('set .') do >>outfile.txt echo %%a %%b

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: How to sort numbers in a text file?

#5 Post by Squashman » 30 Apr 2012 11:56

!k wrote:

Code: Select all

setlocal enableextensions
for /f "tokens=1,2" %%a in (filez.txt) do (
   set "size=............%%b"
   call set %%size:~-12%%=%%a
)
for /f "tokens=1,2 delims==" %%a in ('set .') do >>outfile.txt echo %%a %%b


I like that.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: How to sort numbers in a text file?

#6 Post by Squashman » 30 Apr 2012 12:02

Could change it to this as well if the user wanted it in the same format.

Code: Select all

setlocal enableextensions
for /f "tokens=1,2" %%a in (files.txt) do (
   set "size=............%%b"
   call set %%size:~-12%%=%%a
)
for /f "tokens=1* delims==." %%a in ('set .') do >>outfile.txt echo %%b %%a

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: How to sort numbers in a text file?

#7 Post by MKANET » 30 Apr 2012 12:19

You read my mind, I did want it in the original format; however, I need it from largest to smallest. Any chance you could have it do this in reverse order?

Thanks a million!

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: How to sort numbers in a text file?

#8 Post by !k » 30 Apr 2012 12:54

change
for /f "tokens=1* delims==." %%a in ('set . ^|sort /r') do >>outfile.txt echo %%b %%a

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: How to sort numbers in a text file?

#9 Post by Squashman » 30 Apr 2012 13:00

Still would like to know how the original text file was created. There may have been a way to put it in the correct order from the beginning.

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: How to sort numbers in a text file?

#10 Post by MKANET » 30 Apr 2012 13:03

Awesome, works perfectly!! I really appreciate everyone's help!

@Squashman, filez.txt was created using the forfiles command. forfiles, had very specific functionality that I needed; unfortunately it's sorting capabilities are completely lacking.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: How to sort numbers in a text file?

#11 Post by Squashman » 30 Apr 2012 13:17

MKANET wrote:Awesome, works perfectly!! I really appreciate everyone's help!

@Squashman, filez.txt was created using the forfiles command. forfiles, had very specific functionality that I needed; unfortunately it's sorting capabilities are completely lacking.

Yes I saw that you used forfiles in your initial post. I was hoping to see the code so that we could see if there was possibly another way to do it for you.

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: How to sort numbers in a text file?

#12 Post by MKANET » 12 May 2012 18:34

Ooops!! It looks like the code completely break if there's even one space in the filename!

I should have stated in the first post that these are regular files; which includes filenames with spaces. Such as:

Code: Select all

"a:\The Amazing Race.mpg" 1184769
"a:\The Biggest Loser.mpg" 1184769
"a:\Bad.mpg" 1184769
"a:\Pic 222-555 5 5.jpg" 1308067


If looks like the code presumes there will no spaces at all in the filename; just one space for the default delim.

Code: Select all

for /f "tokens=1,2" %%a in (files.txt) do 


I should have said that the only thing that's 100% expected no matter what, is the quotes around the filename.

Could someone please see if they can convert the below code to be compatible with filenames that have spaces in it? Thanks a million!

Code: Select all

setlocal enableextensions
for /f "tokens=1,2" %%a in (files.txt) do (
   set "size=............%%b"
   call set %%size:~-12%%=%%a
)
for /f "tokens=1* delims==." %%a in ('set . ^|sort /r') do >>outfile.txt echo %%b %%a

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: How to sort numbers in a text file?

#13 Post by MKANET » 12 May 2012 19:15

I'm not sure if this is right; but how about something like the following to handle this?

for /f tokens^=1^,2^ eol^=^"^ delims^=^" %%a in (files.txt)


instead of

Code: Select all

for /f "tokens=1,2" %%a in (files.txt) do 

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

Re: How to sort numbers in a text file?

#14 Post by Aacini » 12 May 2012 19:46

Excuse me. This is my own version of the solution:

Code: Select all

setlocal EnableDelayedExpansion
set /A max=0x7FFFFFFF
for /F "delims=" %%a in (files.txt) do (
   set name=
   for %%b in (%%a) do (
      if not defined name (
         set name=%%b
      ) else (
         set size=%%b
         set /A revSize=max-!size:~0,9!
         set line[!revSize!]=!name! %%b
      )
   )
)
(for /F "tokens=1* delims==" %%a in ('set line[') do echo %%b) > outfile.txt

As usual, this soultion fail if the names may include ! characters, but there is a way to solve that.

Antonio

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: How to sort numbers in a text file?

#15 Post by Squashman » 12 May 2012 22:06

Why not change the way the output of your text file is created and put a true delimiter into the text file to separate the file name from the file size.

Post Reply