Sign in to follow this  
Followers 0
Chris_1013

Filewrite-ing

8 posts in this topic

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 text
  • Close File
Nothing 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? :whistle:

Ta

Chris

Share this post


Link to post
Share on other sites



#2 ·  Posted (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 by Bartokv

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

...However, I'd recommend using Larry's alternative since you're dealing with Netware Shares. His is much more robust and fault-tolerant.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

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