Jump to content

FileOpen repeat until successful?


Recommended Posts

Hi,

I'm writing something to collect machine information that I want to be recorded in a file on a server. My desire is for it to be completely transparent to the users. What I would like for the script to do is retry opening the log file after waiting 1 second if it is unsuccessful in opening the file. This is what I've attempted so far.

While $file = FileOpen("\\servername\logs\servicepackstatus.csv", 1) = -1
    Sleep(1000)
WEnd
FileWriteLine($file, $field1 & "," & $field2 & "," & $field3 & "," & $field4 & "," & $field5 & "," & $field6 & "," & $timestamp)
FileClose($file)
Exit

I get a file handle error when I test with the file open. Ideally it would probably try for a limited number of attempts (10?). I really appreciate any tips.

Link to comment
Share on other sites

Why don't you just do

$file = FileOpen("\\servername\logs\"&@ComputerName&"_servicepackstatus.csv", 2)
FileWriteLine($file, $field1 & "," & $field2 & "," & $field3 & "," & $field4 & "," & $field5 & "," & $field6 & "," & $timestamp)
FileClose($file)
Exit

And then write another script to dump all of the generated CSVs into one master CSV?

Edit: Corrected bug. I still contend that this is the way to do it. Having a large number of machines all try to open a file simultaneously is a bad idea, even if you manage to lock the file open first on each machine.

Edited by lod3n

[font="Fixedsys"][list][*]All of my AutoIt Example Scripts[*]http://saneasylum.com[/list][/font]

Link to comment
Share on other sites

  • Developers

For $x = 1 to 10
    $file = FileOpen("\\servername\logs\servicepackstatus.csv", 1)
    If $file <> -1 then ExitLoop
Next
If $x > 10 then 
    ; unable to open...
Else    
    ; file opened
    FileWriteLine($file, $field1 & "," & $field2 & "," & $field3 & "," & $field4 & "," & $field5 & "," & $field6 & "," & $timestamp)
    FileClose($file)
EndIf
Exit

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

$file = FileOpen("\\servername\logs\"&@ComputerName&"_servicepackstatus.csv", 2) = -1
This doesn't look right at the end of the line .... :shocked:

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

For $x = 1 to 10
    $file = FileOpen("\\servername\logs\servicepackstatus.csv", 1)
    If $file <> -1 then ExitLoop
Next
If $x > 10 then 
    ; unable to open...
Else    
    ; file opened
    FileWriteLine($file, $field1 & "," & $field2 & "," & $field3 & "," & $field4 & "," & $field5 & "," & $field6 & "," & $timestamp)
    FileClose($file)
EndIf
Exit
Perfect! Thanks!
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...