Jump to content
Clark

fileread issue (I think)

Recommended Posts

Hello all

A programme I wrote for the organisation for which I work has been in use for a number of years with no issue.  One of the things that it does is allow for attachments to be selected (Word docs, Excel sheets, etc) which it then writes away as a BLOB in MySQL.

This was all working fine until recently, when suddenly, on the odd occasion, Word would advise that the file was corrupt on retrieval of the BLOB from MySQL.

Upon further examination it appears that the issue is not with the retrieval of the data from MySQL, but reading in the data from the disk by AutoIT.   It seems that on occasions that AutoIT will only read half the file.  

I have no idea why this is, as the whole function is very simple.   I have included a sanitised vesion of it below in the hope that someone can tell me where to look next, as this one has me beat (again).  😋

I have put some comments in to show what I have done to try and solve the problem, but am getting no closer to the truth.  @error is the strangest one - it should return -1 when it reaches end of file but is returning zero. 

Func _AttachAttachment()
   local $file,$chars,$fsize
   If $RFC_Retrieved = False Then
      msgbox(0,"Error","Must have a RFC active first")
   Else
      $file=FileOpenDialog("Attach to RFC",".","All (*.*)",1)
      ; Check if file opened for reading OK
      If $file = -1 Then
         MsgBox(0, "Error", "Unable to open file.")
         Exit
      EndIf
;     FileClose($file)      These two lines are my attempt to open the file in binary mode. Made no difference
;     FileOpen($file,16)
      ; Read in the File
      $fsize = FileGetSize($file)
ConsoleWrite("File size is " & $fsize & @CRLF)  ; the size comes back as what it say in Windows properties
;     $chars = FileRead($file,$fsize)           ; I was using this, then tried the line following this. It didn't help
$chars = FileRead($file)                        ; This is showing a figure $chars/2 - 1    I don't understand this
ConsoleWrite("Number of chars read is " & @extended & " error code is " & @error & @crlf)   ; @error is coming back with 0.  I don't understand this as I thought it would be -1
      FileClose($file)
      EndIf
   EndIf
EndFunc

Thanks in advance for looking at this

Clark

 

Edited by Clark

Share this post


Link to post
Share on other sites

When using FileOpen(), a filehandle is returned which you need to use to read the file, NOT the filename!
Check the helpfile for an example.

Jos

Share this post


Link to post
Share on other sites
27 minutes ago, Jos said:

When using FileOpen(), a filehandle is returned which you need to use to read the file, NOT the filename!
Check the helpfile for an example.

Jos

 

Isn't that what I have done?  $file is the return value (presumably a filehand) from FileOpenDialogue, which I then use in FileRead.   (Or am I misinterpreting what you are saying)

Share this post


Link to post
Share on other sites
1 minute ago, Subz said:

FileOpenDialogue only returns the path of the chosen file.

Right!  I will take the path and pass it to FileOpen and then use the handle in FileRead and see if that works.

Strange my code very worked at all, really!  😂

 

Share this post


Link to post
Share on other sites

You can use either the handle or the filename, however your code included FileClose() which is only for "filehandle", you shouldn't mix the two.  According to the helpfile, using filehandle should be quicker parsing larger files than just using "filename".  WIth regards to your statement about @error returning 0, this is correct behaviour, if you look at the return value for FileRead, a failure will return a non-zero.  The -1 you're referring to is a failure reading a file where it has no content, so only returns the eof.

Share this post


Link to post
Share on other sites

Thanks for everyone's help.   The problem is fixed.

Fixed code is as follows:

$file=FileOpenDialog("Attach to RFC",".","All (*.*)",1)
      $filehnd=FileOpen($file,16)
      ; Check if file opened for reading OK
      If $filehnd = -1 Then
         MsgBox(0, "Error", "Unable to open file.")
         Exit
      EndIf
      ; Read in the File
      $fsize = FileGetSize($file)
      $chars = FileRead($filehnd)
      FileClose($filehnd)

The FileGetSize call not being needed in fact.

Thanks again

Clark

 

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

  • Similar Content

    • By wimhek
      Is it possible , and how can I read and write txt files from Icloud (apple service) ?
      Let me try to explain my application.
      On my Ipad and Iphone I create txt files. On my windows computer it is possible to read and modify these files manually, by logging in on www.icloud.com.
      What I want to make is an auto-it script who reads the txt file and create an new txt file on www.icloud.com,  so I can acces these on my ipad and/or phone.
       
      Thank you.
    • By LWC
      I've made a program that relies on IniReadSectionNames. It reads (~3K) Autorun.inf files in the working folder and creates a GUI based on their contents.
      I made sure to revert to a default GUI upon @error.
      But someone (with Windows XP SP3 32-bit) reported to me he always gets the default menu.
      I sent him a FileRead command instead and it works! So seemingly there's no access problem to AutoRun.inf.
      In the following demo code, I always hit success, but he always ends up with semi-success:
      Local $hIniLocation = "Autorun.inf" Local $aSections = IniReadSectionNames($hIniLocation) If @error Then $aSections = FileRead($hIniLocation) if @error then msgbox(48, "Double error", "Alternative access failed too due to:" & @crlf & @error & @crlf & @extended) else msgbox(0, "Semi-success", "IniReadSectionNames failed, but alternativaly this file contains:" & @crlf & @crlf & $aSections) endif else msgbox(0, "Success", "IniReadSectionNames worked!") endif Why is that? Is there something further to check with him?
      Autorun.inf
    • By antmar904
      Hi,
      I ran a remote program on 180 computers and logged the output to a log file with paexec.  I'm trying to parse through the log file to see which computers launched the program successfully and record the computer name but I'm not sure how to go about it. 
      I can successfully open the log file, read it but not sure how to start separating the data.
      I've attached the log file, basically every computer entry in the log file for a new computer starts with "Connecting to <computername>..." I was thinking about using that as the start and stop point for retrieving the end results.  Anything with a "returned 0" is a success, everything is a fail.
      Any help is much appreciated.
      testlogfile.txt
    • By souldjer777
      Howdy Everyone!
      I've been scouring the forum for something that works... and no dice yet... hoping to stop the frustration - hope someone in the forum knows what to do.
      Basically, I've saved .msg files out of MS Outlook and want to scan each on for email addresses but all I get with fileopen and fileread are symbols... ran through every iteration.
      I've seen people do some soft of binary extract and binarytostring but I haven't gotten that to work either... not sure what's going on.
      Neither of these have worked for me... yes I know they are old and FileOpen has changed... but like I said I tried everything that I know of.
      Please help
       
      $sFile = "C:\temp\messages\Test.msg" $hFile = FileOpen($sFile, 16) ; 16 = binary $binData = FileRead($hFile, 1024) ; read first 1KB FileClose($hFile) ConsoleWrite("Debug: First 1KB = " & $binData & @LF) or...
      $sFile = "C:\temp\messages\Test.msg" $hFile = FileOpen($sFile, 16) $binData = FileRead($hFile, 20480) FileClose($hFile) $startdef=StringTrimLeft(Binary('thetextIamlookingfor'), 20) $enddef=StringTrimLeft(Binary('moretextIamlookingfor'), 20) $binData=StringTrimLeft($binData, StringInStr($binData, $startdef)+StringLen($startdef)-1) $binData=StringLeft($binData, StringInStr($binData, $enddef)-1) ConsoleWrite(BinaryToString('0x'&$binData) & @LF) Thanks!
    • By david1337
      Hi guys
      I hope you can help me out
      I have a script that checks for the current user's loginname in different txt files.
      If the current user's loginname is found in one of the txt files, the script reacts based on that.
      The text files look something like this:
      dle
      oej
      zaq
      oei
       
      (different usernames)
       
      This is an example of the script:
      $username = @UserName ;pulls username that is logged in $Dir = "C:\Test\" Global $Custom[] = ["text01", "text02", "text03", "text04"] ;CHECK LISTS For $i = 0 To UBound($Custom) - 1 $CustomFile = FileOpen($Dir & $Custom[$i] & ".txt", 0) $Readtxt = FileRead($CustomFile) FileClose($CustomFile) If StringRegExp($Readtxt, $username) Then do this and that... EndIf Next It works perfectly if the user logs in as xxxx or XXXX (Both lower and upper case characters)
      So the FileRead function doesn't seem to be case sensitive.
      But if the user logs in as xxXx (mix of lower and upper case characters), then the username is not recognized in the txt file.
       
      Does anyone have a suggestion on how I can fix this?
      Thanks in advance!
      - David
       
×
×
  • Create New...