leuce Posted January 28, 2006 Share Posted January 28, 2006 My question follows on the conversatoin in this topic.I have a text file which I want to sort... not alphabetically, but by line length. I don't know if AutoIt can do this directly, so I devised a method which combines AutoIt with the text editor Metapad (Notepad also works) and finally MS Excel.Basically, I copy each line, gets its length, and then pastes the length of the line at the beginning of the line, plus a tab. Then I save the file manually, open it in Excel, sort by column A, and then delete column A.There is a glitch, however, and I can't figure out what causes it. Sometimes, the script manages to get the line length, but other times it skips a step and then it misgets the line length. Ideally, a text like this:THIS is a HOUSE.this is a CAR.THIS is A Book.should end up like this:16 [tab] THIS is a HOUSE.14 [tab] this is a CAR.15 [tab] THIS is A Book.but sometimes it ends up like this:16 [tab] THIS is a HOUSE.this is a CAR. [tab] 115 [tab] THIS is A Book.Can anyone tell me what I'm doing wrong here? Here is the script:==; The file testing.txt contains a number of lines of text.; This script was designed for Metapad. Metapad must be set; not to display the full path in the file name. Wordwrap; must be off. Use DOS-text (for @CRLF).; To run this script in Notepad, remove Send("+{LEFT}").; GLITCHES; For some reason, on some lines, the script does not return; to the beginning of the line, which results in that line; being counted as 1 character long, and the number being sent; to the end of the line instead of the beginning.; Sets an escape key for emergency purposesHotKeySet("^q", "MyExit"); Gets the number of lines in the text file$file = FileOpen("testing.txt", 0)If $file = -1 ThenMsgBox(0, "Error", "Unable to open file.")ExitEndIf$FileText = FileRead("testing.txt", FileGetSize("testing.txt"))$FileTextARRAY = StringSplit($FileText, @CRLF,1); Wait for user to open file, then go to top of fileWinWaitActive("testing.txt")Send("^{HOME}"); This loop continues for the number of lines in the text fileFor $i = 1 To $FileTextARRAY[0]; Copy the entire line at the cursor positionSend("{HOME}")Send("+{END}")Send("+{LEFT}")Send("^c"); Gets length of the line$line = ClipGet()$length = StringLen($line); Pastes line length and a tab at beginning of the lineClipPut($length)Send("^v")Send("{TAB}")ClipPut($line)Send("^v"); Go to the next lineSend("{DOWN}")NextMsgBox(4096, "Done", "The script is done, mate", 10)Func MyExit() Exit EndFunc ==Or... is there another way to sort a file in AutoIt by line length which either doesn't require using the text editor or using Excel?Thanks in advanceSamuel Murray Link to comment Share on other sites More sharing options...
herewasplato Posted January 28, 2006 Share Posted January 28, 2006 try here:http://www.autoitscript.com/forum/index.php?showtopic=19265http://www.autoitscript.com/forum/index.ph...09entry133209 [size="1"][font="Arial"].[u].[/u][/font][/size] Link to comment Share on other sites More sharing options...
Valuater Posted January 28, 2006 Share Posted January 28, 2006 (edited) well i just had to try #include <file.au3> #include <array.au3> Dim $Old_File = "C:\Test2.txt"; "YOUR OLD FILE NAME" Dim $New_File = "C:\Test2New.txt"; "YOUR NEW FILE NAME" Dim $aRecords If Not _FileReadToArray($Old_File,$aRecords) Then MsgBox(4096,"Error", " Error reading log to Array error:" & @error) Exit EndIf For $x = 1 to $aRecords[0] $aRecords[$x] = (StringLen($aRecords[$x])) & "," & $aRecords[$x] Msgbox(0,'Record:' & $x, $aRecords[$x]); for testing Next $count = $aRecords[0] _ArraySort($aRecords,0,1) _ArrayDisplay($aRecords, "sorted"); for testing $NFile = FileOpen($New_File,2) For $t = 1 to $count $pos = StringInStr($aRecords[$t], ",") FileWriteLine($NFile, (StringTrimLeft($aRecords[$t], $pos))) Msgbox(0,'Record:' & $t, $aRecords[$t]); for testing Next FileClose($NFile) tested ok for me 8) Edited January 28, 2006 by Valuater Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now