Jump to content

Sort lines from a text file


Recommended Posts

I have a text file with links from my RapidShare account.

Unfortunatly, they made some changes to the system and now the exported links is arranged somehow randomly.

I want to put some order in those links, by sorting them:

http:/rapidshare.com/files/397406759/intytyut.rar
http:/rapidshare.com/files/387092432/Adsfdfg.rar
http:/rapidshare.com/files/397116908/Algdf.rar
http:/rapidshare.com/files/397111430/Svdffgd.rar
http:/rapidshare.com/files/397111430/79033tyry.rar
http:/rapidshare.com/files/397109320/fgdfg.rar
http:/rapidshare.com/files/397398703/adsasdaasda.rar
http:/rapidshare.com/files/397398703/45000.rar
http:/rapidshare.com/files/397397493/eeeerte.rar
http:/rapidshare.com/files/374707792/Ddsfs.rar

to became:

http:/rapidshare.com/files/397398703/45000.rar
http:/rapidshare.com/files/397111430/79033tyry.rar
http:/rapidshare.com/files/397398703/adsasdaasda.rar
http:/rapidshare.com/files/387092432/Adsfdfg.rar
http:/rapidshare.com/files/397116908/Algdf.rar
http:/rapidshare.com/files/374707792/Ddsfs.rar
http:/rapidshare.com/files/397397493/eeeerte.rar
http:/rapidshare.com/files/397109320/fgdfg.rar
http:/rapidshare.com/files/397406759/intytyut.rar
http:/rapidshare.com/files/397111430/Svdffgd.rar

I have deleted a "/" from "http://" in order to work with the script (made by Smoke'n) founded in an old topic.

#include <file.au3>
#include <array.au3>

$InFile = @ScriptDir & "\TestIn.txt"
$Outfile = @ScriptDir & "\TestOut.txt"

Dim $avInput[1] = [0], $avSplit[1] = [0], $avSort[1] = [0], $avOutput[1] = [0]
; Read file to array
If _FileReadToArray($InFile, $avInput) Then
    ; Evaluate each line
    For $n = 1 To $avInput[0]
        $avSplit = StringSplit($avInput[$n], "/")
        If $avSplit[0] > 1 Then
            ; Create a sortable index based on fifth field
            _ArrayAdd($avSort, $avSplit[5] & "||" & $n) ; 'Double pipe' delimits index number
        EndIf
    Next
    $avSort[0] = UBound($avSort) - 1
    
    If $avSort[0] > 0 Then
        ; sort the index
        _ArraySort($avSort, 0, 1) ; Ascending, start from [1]
        
        ; Use sorted index to copy lines from $avInput to $avOutput
        For $n = 1 To $avSort[0]
            $avSplit = StringSplit($avSort[$n], "||")
            _ArrayAdd($avOutput, $avInput[$avSplit[$avSplit[0]]])
        Next
        $avOutput[0] = UBound($avOutput) - 1
        
        ; Write to output file
        If _FileWriteFromArray($Outfile, $avOutput, 1) Then
            ; Open file to display results
            Run("notepad.exe " & $Outfile)
            Exit
        Else
            MsgBox(16, "Error", "Error writing to file: " & $Outfile) ; write from [1] to end
        EndIf
    Else
        MsgBox(16, "Error", "No valid lines found to sort.")
    EndIf
Else
    MsgBox(16, "Error", "Error reading file: " & $InFile)
EndIf

The problem with this script is that "eat" 3 lines from the output (TestIn.txt has 5263 lines and TestOut.txt has 5260 lines).

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...