Jump to content

Help With FileReadLine & FileWriteLine


Recommended Posts

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 by cheesestain
Link to post
Share on other sites

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 by Spiff59
Link to post
Share on other sites

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)
Link to post
Share on other sites

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 2021-06-05 - Version 1.5.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (NEW 2021-06-14 - Version 1.6.5.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 (NEW 2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to post
Share on other sites

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

Link to post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...