# Sorting a TXT file

## Recommended Posts

This example shows an other application using ADOR.Recordset.

Sorting a text file using this approach :

Save this as a TXT file

e

V

B

A

d

c

And sort the content using the following script.

Const $adVarChar = 200 Const$MaxCharacters = 255
Const $ForReading = 1 Const$ForWriting = 2
Const $Col = "Name"$DataList = ObjCreate("ADOR.Recordset")
$DataList.Fields.Append ($Col, $adVarChar,$MaxCharacters)
$DataList.Open()$objFSO = ObjCreate("Scripting.FileSystemObject")
$objFile =$objFSO.OpenTextFile("C:\Test.txt", $ForReading) Do$strLine = $objFile.ReadLine$DataList.AddNew()
$DataList.Fields($Col).Value = $strLine Msgbox(0,"Debug",$strLine)
$DataList.Update() Until$objFile.AtEndOfStream()

$objFile.Close()$DataList.Sort = $Col If Not$DataList.Bof Then
$DataList.MoveFirst() EndIf Do Local$strText
$strText &=$DataList.Fields($Col).Value & @CRLF Msgbox(0,"Debug",$strText)
$DataList.MoveNext() Until$DataList.EOF()

$objFile =$objFSO.OpenTextFile("C:\Test.txt", $ForWriting)$objFile.WriteLine ($strText)$objFile.Close()

Enjoy.

##### Share on other sites

Nice, but couldn't you just read the file with FileRead() then StringSplit() it and put it into the recordset? That way you wouldn't have to have one character per line.

##### Share on other sites

Hi @ptrex,

Thanks again for that new approach!

Can you describe the likely usefulness in terms of

A. speed and

B. practicality?

eg vs.

1. Autoit File read to array/ Arraysort/ FileWriteFromArray.

2. dos file sort.

3. vbs file sort?

Best, Randall

##### Share on other sites

btw,

I get this error when reading back from the database to make the text file;

Does it need error checking for a large file, or too big a string, or too big a data entry, or similar? ?write line by line?

randall

C:\Programs\SearchEngine\SortPtrexAdor.au3 (46) : ==> The requested action with this object has failed.:

$DataList.Fields($Col).Value = $strLine$DataList.Fields($Col).Value =$strLine^ ERROR

!>AutoIT3.exe ended.rc:2147483647

##### Share on other sites

@erifash

Nice, but couldn't you just read the file with FileRead() then StringSplit() it and put it into the recordset?

Yes I could but this is just a quick VBS converted script as you can see.

The purpose of this example is only to show the possibilities of what ADOR.RecordSet can do.

@Randallc

##### Share on other sites

Hi,

The error was in your script when I read a file; fixed it with

"Const \$MaxCharacters = 511" as the file had lines >255.

But it is 20x slower than a DOS sort [7Mb test file; not much difference if<300Kb]; I suppose it may have interest as a method, but I can't see an application when it is so slow?...

Best, Randall

Edited by randallc

## Create an account

Register a new account