1,105,286 Community Members

Windows CMD

Member Avatar
shanie7
Newbie Poster
3 posts since Jan 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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 ;)

Member Avatar
DavidKroukamp
Master Poster
737 posts since Dec 2011
Reputation Points: 105 [?]
Q&As Helped to Solve: 180 [?]
Skill Endorsements: 4 [?]
Team Colleague
Featured
 
0
 

maybe try the for command?

Member Avatar
InterociterOper
Newbie Poster
3 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
InterociterOper
Newbie Poster
3 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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>

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article