Jump to content

Reducing time taken to do a FileRead


Recommended Posts

Hi. In a program I'm writing I have to assign the contents (source) of an HTML file to a variable and I have to know exactly how many characters there are in the file. As a base for the code below, I used the code in the AutoIt3 help file for FileRead.

$file = FileOpen("somefile.shtml", 0)
While 1
    $chars = FileRead($file, 1)
    $fileContents = $fileContents & $chars
    If @error = -1 Then ExitLoop
Wend
$numCharsread = StringLen($fileContents)

The HTML file has tens of thousands of characters in it, so the process of reading takes quite some time (2 to 3 minutes on my PIII 550 Mhz/320 Mb RAM PC). I'm wondering if there's a much faster way of getting the file's text assigned to a variable. I'm thinking that if there's no better way, I could have Notepad open the file, do a Select All, do a copy with CTRL-C, then assign CipGet to a variable. The problem with that is it's hardly transparent to the user, since they'd have to see Notepad opening, the Select All taking place, and Notepad closing. Plus, it's my understanding that anytime you have to depend on sending keystrokes, it's a risk.

One of FileRead's parameters is the number of characters to read. Is there a way to specify that all characters in the file should be read, so looping through the characters one by one is not necessary?

Please advise. :)

Link to comment
Share on other sites

Thank you.  Since FileGetSize returns the file size in bytes, is the size in bytes the same as the total number of characters in the file?  I know that's a basic computer question I should know, but I don't.

<{POST_SNAPBACK}>

Yes. MHz's post was very clear about that.
Link to comment
Share on other sites

One note. The code

$chars = FileRead($file, FileGetSize($file))
provided by MHz ddn't work using $file in FileGetSize($file), i.e., the second instance of $file needed to be replaced with an actual filename. Once I did so, it worked great. Thank you MHz and SlimShady. Edited by egalvez
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...