Hi,

I have a text file here.

Names Fishingclub Totalweight (g)
Kalle Karlsson : Krokodilen : 1035
Sven Svensson : Noten : 786
Anders Andersson : Handgranaten : 1234
Jonas Jonasson : Uttern : 1497
Sten Stensson : : 654
Turliga Ture : : 1498
Donald Donaldsson : Noten : 987

I want to use this text file in the windows CMD, and sort by name and weight, Highest to lowest.

I tried the sort command, but it doesn't do what i want it to do. Probably need to combine the sort command with another command.

Need som help please ;)

Recommended Answers

All 3 Replies

maybe try the for command?

<code>
@echo off
cls
title Sorting file by score... Interociter 2011
echo/
echo Sorting file content by score...
echo/
echo Let's create the file we want to sort and call it data.txt
echo/==========================================================
dir c:\windows\system32\*.* /s > nul
dir c:\windows\system32\*.* /s > nul
dir c:\windows\system32\*.* /s > nul
echo on
echo Names Fishingclub Totalweight (g) > data.txt
@echo off
dir c:\windows\system32\*.* /s > nul
@echo on
echo Kalle Karlsson : Krokodilen : 1035 >> data.txt
@echo off
dir c:\windows\system32\*.* /s > nul
@echo on
echo Sven Svensson : Noten : 786 >> data.txt
@echo off
dir c:\windows\system32\*.* /s > nul
@echo on
echo Anders Andersson : Handgranaten : 1234 >> data.txt
@echo off
dir c:\windows\system32\*.* /s > nul
@echo on
echo Jonas Jonasson : Uttern : 1497 >> data.txt
@echo off
dir c:\windows\system32\*.* /s > nul
@echo on
echo Sten Stensson : : 654 >> data.txt
@echo off
dir c:\windows\system32\*.* /s > nul
@echo on
echo Turliga Ture : : 1498 >> data.txt
@echo off
dir c:\windows\system32\*.* /s > nul
@echo on
echo Donald Donaldsson : Noten : 987 >> data.txt
@echo off
echo/
dir c:\windows\system32\*.* /s > nul
dir c:\windows\system32\*.* /s > nul
echo/...and...
dir c:\windows\system32\*.* /s > nul
echo/
echo/
echo Here is the file.... DATA.TXT Just like in the example...
echo/
echo/
echo/
type data.txt
echo/
echo/
echo/
pause
echo/
echo/
echo/
echo/
echo Now we'll save the title line out.
echo/
echo/ (type data.txt ' find "Fishingclub Totalweight" - title.tmp)
echo/
echo/
echo/
echo/
dir c:\windows\system32\*.* /s > nul
echo/
echo/
type data.txt | find "Fishingclub Totalweight" > title.tmp
type title.tmp
echo/
echo/
echo There...now...
echo/
dir c:\windows\system32\*.* /s > nul
dir c:\windows\system32\*.* /s > nul
echo ...using the original file contents...
echo/
echo/
echo/
dir c:\windows\system32\*.* /s > nul
type data.txt
echo/
echo/
pause
echo/
echo/
echo/
echo/
echo/
echo Rearrange the contents and pad the varied shorter numbers...
echo/
echo/ (for /f "skip=2 tokens=1,2,3 delims=:" %%p in (data.txt) do set /a score=1000000000+%%r &echo !score! : %%q : %%p)
echo/
echo/
echo/
echo/
pause
echo/
echo/
::math happens here... :-P
:: must delay expansion or forindo gets lazy...
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "skip=2 tokens=1,2,3 delims=:" %%p in (data.txt) do set /a score=1000000000+%%r &echo !score! : %%q : %%p >> tempsort.tmp
type tempsort.tmp
echo/
echo/
pause
echo/
echo/
echo Then let's sort them in ascending order with a simple sort command....
echo/
echo/ (sort tempsort.tmp - ascending.tmp)
echo/
echo/
echo/
echo/
pause
echo/
echo/
:sort /+13 tempsort.tmp |
sort tempsort.tmp > ascending.tmp
type ascending.tmp
echo/
pause
echo/
echo/
echo/
echo Then subtract the padding and put them back the way they were, but sorted....
echo/
echo/ (for /f "tokens=1,2,3 delims=:" %%p in (ascending.tmp) do set /a score=%%p-1000000000 & echo %%r : %%q : !score!)
echo/
echo/
echo/
echo/
pause
::more math...:-P
if exist final.txt del final.txt
for /f "tokens=1,2,3 delims=:" %%p in (ascending.tmp) do set /a score=%%p-1000000000 & echo %%r : %%q : !score! >> final.txt
echo/
echo/
type final.txt
echo/
echo/
dir c:\windows\system32\*.* /s > nul
echo .... almost forgot the title line.... :-O
echo/
echo/
type title.tmp
echo/
dir c:\windows\system32\*.* /s > nul
if exist TA-DA.txt del TA-DA.txt
copy title.tmp+final.txt TA-DA.txt
echo/
echo/
::yeah, I was bored. How did you know?
dir c:\windows\system32\*.* /s > nul
echo/
echo ...and...
dir c:\windows\system32\*.* /s > nul
dir c:\windows\system32\*.* /s > nul
title Close the notepad file when you are done reading and this program will end.
notepad TA-DA.txt
del title.tmp
del tempsort.tmp
del ascending.tmp
del final.txt
title The program is finished. You may exit.
cls

<code>

Shorter version. SKIP statement should say "skip=1" in previous answer (my original data.txt had an extra blank line at the beginning)

<code>
@echo off
cls
echo/
echo Short Sort (assumes data is in a file called DATA.txt)
echo/
type data.txt | find "Fishingclub Totalweight" > title.tmp
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "skip=1 tokens=1,2,3 delims=:" %%p in (data.txt) do set /a score=1000000000+%%r &echo !score! : %%q : %%p >> tempsort.tmp
sort /+13 tempsort.tmp |sort tempsort.tmp > ascending.tmp
if exist final.txt del final.txt
for /f "tokens=1,2,3 delims=:" %%p in (ascending.tmp) do set /a score=%%p-1000000000 & echo %%r : %%q : !score! >> final.txt
if exist TA-DA.txt del TA-DA.txt
copy title.tmp+final.txt TA-DA.txt
notepad TA-DA.txt
del title.tmp
del tempsort.tmp
del ascending.tmp
del final.txt
<code>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.