cheesestain Posted May 5, 2012 Posted May 5, 2012 (edited) I think I'm doing something wrong. I have the following code which works but when I try to read/write a file with 10000 lines it takes a really long time to process. Is my code ok? $File = GUICtrlRead($NewFileInput) $RIMPINFile = FileOpen("RIMPIN.txt",2) FileOpen($File,0) For $i = 2 to _FileCountLines($File) $line = FileReadLine($File, $i) $RIMPINLine = StringStripWS(StringMid($line,181,10),8) FileWriteLine($RIMPINFile,$RIMPINLine) Next ClipPut(FileRead($RIMPINFile)) MsgBox(0,"Copied",_FileCountLines($File) - 1 & " RIM PIN's Copied To Clipboard.") FileClose($File) FileClose($RIMPINFile) EndIf Edited May 5, 2012 by cheesestain
Spiff59 Posted May 5, 2012 Posted May 5, 2012 (edited) I'm not seeing anything wrong at first glance.With 20K+ separate I/O function calls it's bound to take some time.I'd suggest using _FileReadToArray() to read the file in one call, then process the array, and write it back out with _FileWriteFromArray(). You can also dispense with the _FIleCountLines() calls, as FRTA returns that in the first element.Edit: On second glance, you are doing something wrong (or at least very inefficient) as well. Your variable $file is the string of the filename, not a file handle returned from a FileOpen() call (like $RIMPINFile), so each FileReadLine() you call is opening, reading, and closing the file each time. You should store the handle of the second FileOpen() and use it in the FileReadLine(). That would eliminate a ton of opens and closes. But... I'd still go with my first suggestion to use FRTA. Edited May 5, 2012 by Spiff59
cheesestain Posted May 5, 2012 Author Posted May 5, 2012 Thanks, this seems to work a lot better. $File = GUICtrlRead($NewFileInput) _FileReadToArray($File,$aRecords) $RIMPINFile = FileOpen("RIMPIN.txt",2) For $i = 2 to _FileCountLines($File) $RIMPINLine = StringStripWS(StringMid($aRecords[$i],181,10),8) FileWriteLine($RIMPINFile,$RIMPINLine) Next ClipPut(FileRead($RIMPINFile)) MsgBox(0,"Copied",_FileCountLines($File) - 1 & " RIM PIN's Copied To Clipboard.") FileClose($File) FileClose($RIMPINFile)
water Posted May 5, 2012 Posted May 5, 2012 This should be faster because _FileCountLines reads the file a second time to calculate the number of lines.$File = GUICtrlRead($NewFileInput) _FileReadToArray($File, $aRecords) $RIMPINFile = FileOpen("RIMPIN.txt",2) For $i = 2 to $aRecords[0] $RIMPINLine = StringStripWS(StringMid($aRecords[$i],181,10),8) FileWriteLine($RIMPINFile,$RIMPINLine) Next ClipPut(FileRead($RIMPINFile)) MsgBox(0,"Copied",_FileCountLines($File) - 1 & " RIM PIN's Copied To Clipboard.") FileClose($File) FileClose($RIMPINFile) My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
kylomas Posted May 5, 2012 Posted May 5, 2012 cheesestrain, If you do not need the output file try this $File = GUICtrlRead($NewFileInput) _FileReadToArray($File, $aRecords) For $i = 2 to $aRecords[0] $aRecords[$i] = StringStripWS(StringMid($aRecords[$i],181,10),8) & '`' Next ClipPut(_ArrayToString($aRecords,'`')) MsgBox(0,"Copied",$aRecords[0] - 2 & " RIM PIN's Copied To Clipboard.") kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill
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