Jump to content

FileOpen($logFile, 0) returns error


Recommended Posts

Hello all,

I am trying to open a log file used by my application. If the application is running FileOpen($logFile, 0) returns error. If the application is closed, FileOpen($logFile, 0) works just fine. While the application is running FileOpen($logFile, 2) works OK, but I don't want to erase the content of the file. My only interest is reading the content of it without any other change.

I've added Everyone as an user allowed to operate changes on the log file(with full list of permissions) but it still does not work.

The Help file says: "A file may fail to open due to access rights or attributes." How can I change them dynamically to allow my script readonly access to the log file while the application is running? I've tried searching on the forum but I could not find something for this particular situation.

Thank you,

Ionut

Link to comment
Share on other sites

You would have to change the application. When it opens the file, it sets an attribute for sharing mode (dwShareMode). If dwShareMode = 0 the file is locked until the app closes it.

:blink:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

You would have to change the application. When it opens the file, it sets an attribute for sharing mode (dwShareMode). If dwShareMode = 0 the file is locked until the app closes it.

:blink:

Hmmm...interesting!!!And how does Mode "2 = Write mode (erase previous contents)" manage to open up the file? It is erasing the original file and then creating a brand new one with the same name as the original?

Thanks,

Ionut

Edited by ionut
Link to comment
Share on other sites

Yeap, the circumstances are exactly the same. I am just changing the Mode from 0 to 2 and vice versa while my application is running. Do you think something is broken with my application or with the AutoIT FileOpen function?

Thanks,

Ionut

Link to comment
Share on other sites

And the file accualy exist when you run "FileOpen($logFile, 0)" ?

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

And the file accualy exist when you run "FileOpen($logFile, 0)" ?

Yes, the file is definitely there. If I close the application then FileOpen($logFile, 0)will be successful.

Thanks,

Ionut

Link to comment
Share on other sites

Yes, the file is definitely there. If I close the application then FileOpen($logFile, 0)will be successful.

lol, Thats not working for me.

That still allows for the possibility the file is accually not there when your "FileOpen($logFile, 0)" fails.

I suggest you drop in some code so people can see what your actually doing code wize.

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

Fair enough...This is the code I am using

$path="C:\Documents and Settings\Admin05\Local Settings\Application Data\Ixia\IxLoad\5.10.151.23\Logs\ixload-4-00.log"
$file = FileOpen($path, 0)
; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
$aSplit = StringSplit(StringStripCR(FileRead($path)), @LF)
_ArrayDisplay($aSplit)
FileClose($path)

If the application is:

1. running - then the "Unable to open file" message box appears

2. closed - the array is displayed correctly

Thanks,

Ionut

Link to comment
Share on other sites

Ok.

Run/Use following code and report back what the error is you get.

$sFileSpec = "C:\Documents and Settings\Admin05\Local Settings\Application Data\Ixia\IxLoad\5.10.151.23\Logs\ixload-4-00.log"
If FileExists($sFileSpec) And Not StringInStr(FileGetAttrib($sFileSpec), 'D') Then
    $hFile = FileOpen($sFileSpec, 0)
    ; Check if file opened for reading OK
    If $hFile = -1 Then
        MsgBox(0, "Error_2", "Failed to open file." & @CRLF & 'file: ' & $sFileSpec)
        Exit
    EndIf
    $aSplit = StringSplit(StringStripCR(FileRead($hFile)), @LF)
    _ArrayDisplay($aSplit)
    FileClose($hFile) ;; <- You where using $sFileSpec/$path here. should have been $hFile. (So this file was not closed.)
Else
    MsgBox(0, "Error_1", "File not found." & @CRLF &  'file: ' & $sFileSpec)
    Exit
EndIf
Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

Just my opinion ..

1. Your application has a file-lock on your log.

2. I see no reason why you need "fileopen/fileclose". Just use "fileread" directly.

3. If "fileread" fails .. see number 1.

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

Cool. Thanks.

At least we now know for sure the file is really there when the "FileOpen($logFile, 0)" fails. (unless my code was messed up ...)

"Mode 0 fails, but mode 2 works under the exact same circumstances?"

Will need to give this some more though.

@general: Anyone with some other info or points of view for this case?

(thats 1 ...)

Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

This works ..

#include <Array.au3>

Local $log = "C:\Documents and Settings\Admin05\Local Settings\Application Data\Ixia\IxLoad\5.10.151.23\Logs\ixload-4-00.log"

If FileExists($log) Then; Check if file exists
    FileRead($log)
    If @error = 1 Then; Check if locked
        MsgBox(0, '', 'File is locked')
    Else
        Local $aSplit = StringSplit(StringStripCR(FileRead($log)), @LF)
        _ArrayDisplay($aSplit)
    EndIf
EndIf
Edited by ripdad

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

Hi,

FileRead() fails as well when application is running. The application is creating the log file in python then performs open(filename, "a+") to append. From what I've searched the internet, appending the file like this in python should not set dwsharemode to 0. BTW, I can double click the file and open it in Notepad.

I've tried to use Process Explorer to check out the way the log file was opened but I am seeing only the user permissions.

Thanks,

Ionut

Link to comment
Share on other sites

okay .. since notepad can open it .. try this as a test:

#include <Array.au3>

Local $log = "C:\Documents and Settings\Admin05\Local Settings\Application Data\Ixia\IxLoad\5.10.151.23\Logs\ixload-4-00.log"

Local $aSplit, $templog = @TempDir & '\templog.log'

FileCopy($log, $templog, 1)

If FileExists($templog) Then
    $aSplit = StringSplit(StringStripCR(FileRead($templog)), @LF)
    _ArrayDisplay($aSplit)
Else
    MsgBox(0, '', 'File Not Found')
EndIf

- Edit: one too many EndIf's

Edited by ripdad

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

okay .. since notepad can open it .. try this as a test:

#include <Array.au3>

Local $log = "C:\Documents and Settings\Admin05\Local Settings\Application Data\Ixia\IxLoad\5.10.151.23\Logs\ixload-4-00.log"

Local $aSplit, $templog = @TempDir & '\templog.log'

FileCopy($log, $templog, 1)

If FileExists($templog) Then
        $aSplit = StringSplit(StringStripCR(FileRead($templog)), @LF)
        _ArrayDisplay($aSplit)
    EndIf
Else
    MsgBox(0, '', 'File Not Found')
EndIf

Hi,

This script is working - it is actually the workaround I have been using for a while. The thing it that I have to copy the log file continuously to check for updates, which is not very elegant. And I was interested in finding the technical details of why the FileOpen() function fails. If I'll not get another answer then this workaround is good enough.

Thanks a lot,

Ionut

Link to comment
Share on other sites

Well good.

I've had problems with the "Application Data" folder myself on several occasions.

Of those instances .. I've had to do some type of work-around.

Sorry, can't tell you why exactly. (technically wise)

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

The thing it that I have to copy the log file continuously to check for updates, which is not very elegant.

Not really needed. Some alternatives.

- if possible, turn of the archive flag on the log file after you made a copy of it. (and scan for it being active again.)

- or monitor the file size. (Pickup file size of copied file, and compare to current log file for change.)

Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

MvGulik,

I like your suggestions but I don't think it would help me. The log is very verbose and changes every time the application is doing something(meaning 99% of run time). If I add another If statement there it will only increase the number of computations without a real benefit. But I will keep that in mind as a best practice in the future.

Thanks a lot,

Ionut

PS. I love Escher's work as well:).

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