Sign in to follow this  
Followers 0
cheesestain

Help With FileReadLine & FileWriteLine

6 posts in this topic

#1 ·  Posted (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 by cheesestain

Share this post


Link to post
Share on other sites



#2 ·  Posted (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 by Spiff59

Share this post


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)

Share this post


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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


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

Share this post


Link to post
Share on other sites

Thanks for your help guys!

Share this post


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
Sign in to follow this  
Followers 0