Chris_1013 Posted March 3, 2004 Share Posted March 3, 2004 I've written the code below (script-let, not whole script)$file = FileOpen($DIR & "\Text.txt",2) While $file = -1 Then While MsgBox(4149,"Error","Could not open file for writing") = 4 And $file = -1 $file = FileOpen($DIR & "\Text.txt",2) Wend MsgBox(4149,"Error","Could not open file for writing") Exit Wend While Not FileWrite($file,$Config) If MsgBox(4149,"Error","Could not write to file") = 2 Then Exit EndIf Wend FileClose($file)What I'm trying to do is:Open a file for writing (over-write if file exists)Write a line of textClose FileNothing too complex, but I'm trying to wrap it in error checking so if it doesn't work the user can retry or cancel, as I think this would be more precise.Does this look OK how I've coded it? Can anyone recommend a more slevete way of doing this? TaChris Link to comment Share on other sites More sharing options...
Bartokv Posted March 3, 2004 Share Posted March 3, 2004 (edited) I haven't tested the following code, but it should work:$file = -1 ; Assign a default "fail" state to $file While $file == -1 $file = FileOpen($DIR & "\Text.txt",2) If MsgBox(4149,"Error","Could not open file for writing") <> 4 Then ExitLoop EndIf Wend While Not FileWrite($file,$Config) If MsgBox(4149,"Error","Could not write to file") <> 4 Then ExitLoop EndIf Wend FileClose($file)Hope this helps!Edit: BTW: I'm not sure why you had the logic around the FileWrite segment. Once you have write access to the file, you shouldn't lose it... But I left it in just in case you're able to lose write access due to strange file sharing permissions within your network environment. Edited March 3, 2004 by Bartokv Link to comment Share on other sites More sharing options...
Chris_1013 Posted March 3, 2004 Author Share Posted March 3, 2004 OK, I see what you're saying that by rights you shouldn't need logic around FileWrite. Considering I'm writing this to replace IniWrite because it doesn't work on Netware shares, I just wanted to be as precise and exact as possible. The code you've sent looks better, but there is a problem in that it will always give the error message the first time, even if it could open the file. Link to comment Share on other sites More sharing options...
Administrators Jon Posted March 3, 2004 Administrators Share Posted March 3, 2004 Tsssss. Relying on the default value of a Dim being zero. Safer would be: Dim $success = 0 before the loop. Or possibly one of those new-fangled do...until loops Deployment Blog: https://www.autoitconsulting.com/site/blog/ SCCM SDK Programming: https://www.autoitconsulting.com/site/sccm-sdk/ Link to comment Share on other sites More sharing options...
Bartokv Posted March 3, 2004 Share Posted March 3, 2004 The code you've sent looks better, but there is a problem in that it will always give the error message the first time, even if it could open the file.Oops... Like I said, it wasn't tested. I forgot to include the other if statement within the first loop: Dim $file = -1 ; Assign a default "fail" state to $file While $file == -1 $file = FileOpen($DIR & "\Text.txt",2) If $file == -1 Then If MsgBox(4149,"Error","Could not open file for writing") <> 4 Then ExitLoop EndIf Wend ... To follow standard coding practices I also Dimmed the first $file variable... I'm not use to Dimming variables, so I keep forgetting to use it. ...However, I'd recommend using Larry's alternative since you're dealing with Netware Shares. His is much more robust and fault-tolerant. Link to comment Share on other sites More sharing options...
Valik Posted March 3, 2004 Share Posted March 3, 2004 Keep in mind == FORCES a string compare. I don't see where that would be a problem, but it would probably be safer if that second line were "While $file = -1". Link to comment Share on other sites More sharing options...
Bartokv Posted March 5, 2004 Share Posted March 5, 2004 Keep in mind == FORCES a string compare. I don't see where that would be a problem, but it would probably be safer if that second line were "While $file = -1".Thanks, old habbits of == for compare die hard. Link to comment Share on other sites More sharing options...
Chris_1013 Posted March 5, 2004 Author Share Posted March 5, 2004 Good stuff, thanks for the help. I know it's probably a little over-kill, but I just like to check for ever eventuality when I code so I actually never normally get the thing done! I like the idea of Larry's using the sort of 'follow through' success value, so that an error will stop the rest of the stuff processing and just give an error. I guess I was being pernickity trying to give different errors for different problems. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now