Jump to content

error handling with Fileread function


Recommended Posts

Hi

here is my pb, I have to read some files. My script works well but when files are big (generally over 150 Mb, it depends of the computer/state of memory...) I got a message error : "Error allocating memory." and the script end

I'd like to know if its possible to prevent this message box and simply handle error in my script so I could skip this file.

$Result = Test("c:\mybigfile")
ConsoleWrite($Result & @CR)

Func Test($sFilePath)
    $hFile = FileOpen($sFilePath,16)
    If $hFile = -1 Then
        SetError(1)
        Return 0
    EndIf
    $s_Line=FileRead($hFile,FileGetSize($sFilePath))
; here is my error message "Error allocating memory."
; the scripts ends and console is AutoIT3.exe ended.rc:1
    If @error = -1 Then
        ConsoleWrite("error detected" & @CR)
        FileClose($hFile)
        Return "error detected"
    EndIf
    FileClose($hFile)
    Return $s_Line
EndFunc  ;==>Test
Edited by baghenamoth
Link to comment
Share on other sites

Hi

here is my pb, I have to read some files. My script works well but when files are big (generally over 150 Mb, it depends of the computer/state of memory...) I got a message error : "Error allocating memory." and the script end

I'd like to know if its possible to prevent this message box and simply handle error in my script so I could skip this file.

$Result = Test("c:\mybigfile")
ConsoleWrite($Result & @CR)

Func Test($sFilePath)
    $hFile = FileOpen($sFilePath,16)
    If $hFile = -1 Then
        SetError(1)
        Return 0
    EndIf
    $s_Line=FileRead($hFile,FileGetSize($sFilePath))
; here is my error message "Error allocating memory."
; the scripts ends and console is AutoIT3.exe ended.rc:1
    If @error = -1 Then
        ConsoleWrite("error detected" & @CR)
        FileClose($hFile)
        Return "error detected"
    EndIf
    FileClose($hFile)
    Return $s_Line
EndFunc  ;==>Test
FileGetSize?
Link to comment
Share on other sites

Hello baghenamoth,

I believe what Bert is implying ( Bert: please correct me if I am wrong ) is that you should test the size of the file prior to opening it. If the file size is problematic for your environment, then do not open it.

What I believe you are asking is if there is any structured error handling ( like "try...catch" blocks ) that can by employed in AutoIt scripts. I don't believe that functionality exists. Perhaps some industrious individuals on this forum have invented a way, but I have yet to see it.

Have you been able to re-create this error on any other machines using the same files?

Zach...

Link to comment
Share on other sites

Hello baghenamoth,

I believe what Bert is implying ( Bert: please correct me if I am wrong ) is that you should test the size of the file prior to opening it. If the file size is problematic for your environment, then do not open it.

yes i could do this but the size depends of the machine (cpu/memory...) and I need to open read and modify those files.

What I believe you are asking is if there is any structured error handling ( like "try...catch" blocks ) that can by employed in AutoIt scripts. I don't believe that functionality exists. Perhaps some industrious individuals on this forum have invented a way, but I have yet to see it.

this is exactly what I'me looking for..

Have you been able to re-create this error on any other machines using the same files?

Zach...

oh yes, many many time ;-)

anyway I found a solution :

calling a first exe waiting for error message

for each file (generally less than 100) calling a second exe which is going to open it, doing some stuff and returns ok if everithing is ok

when this second exe doesn't return ok I try an alternative : opening and reading the file line by line instead of opening it in one way

yes it's a little bit "tiré par les cheveux" as we say in french, but it works

anyway, thx for your answers

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...