Sign in to follow this  
Followers 0
noobomatic

FileOpen repeat until successful?

5 posts in this topic

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.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

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]

Share this post


Link to post
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


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

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

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
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!

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
Sign in to follow this  
Followers 0