Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extended KotD #8: _READFILE$
#4
@RhoSigma That "File Not Found" *is* an internal existence check.  It tells you if the file isn't found, and then exits the process for you.  Let me see if I can illustrate a little better what I'm trying to highlight here:

Code: (Select All)
Open "temp.txt" For Output As #1: Close 'create a file
Kill "temp.txt" '                        so we can kill that file to make certain it doesn't exist, without errors

If _FileExists("temp.txt") Then Print "Somethings wrong!  We kilt it and it didn't die!"
Open "temp.txt" For Binary As #1
temp$ = Space$(LOF(1))
Get #1, 1, temp$
Close

If _FileExists("temp.txt") Then
    Print "Even though there was no temp.txt, there is now!  OPEN FOR BINARY made one!"
Else
    Print "File still missing.  Open For Binary didn't make one."
End If

Kill "temp.txt" '              kill that file
If _FileExists("temp.txt") Then Print "Somethings wrong!  We kilt it and it didn't die!"

temp$ = _ReadFile$("temp.txt") ' and now readfile$ will toss an error for "File Not Found"

If _FileExists("temp.txt") Then
    Print "Even though there was no temp.txt, there is now!  ReadFile$ made one!"
Else
    Print "File still missing, as ReadFile didn't make one."
End If

The old method would automatically CREATE a file when you OPEN FOR BINARY, if none existed before.  That file would be size 0 in length; you'd read a 0-byte string, and then once you close the file, you're left with a size-0 file on your drive which you'd probably need to clean up.

With _ReadFile$, it checks to see if the file exists, and if it doesn't find it where you specified, it tosses the "File Not Found" error and then exits, returning you a 0-byte string if you click "Continue".  What it doesn't do is create that file on your drive anywhere, so you never have to worry about cleaning up any junk files from your drive.  Wink

That "File Not Found" *is* a form of an existence check...  at least, that's what I'd consider it to be.  I suppose someone else might consider it to be an ERROR.  I guess it's all just a matter of perspective on how you consider things.  Smile

What our new little command does, is actually something very similar to all the below here:

Code: (Select All)
Dim f As Long
Dim temp$

If _FileExists("temp.txt") = 0 Then
    _MessageBox "ERROR -- File Not Found", "Error!  File Not Found", "error"
Else
    f = FreeFile
    Open "temp.txt" For Binary As #f
    temp$ = Space$(LOF(f))
    Get #f, 1, temp$
    Close #f
End If

That's a pretty good reduction for a good bit of typing/coding, for one quick shortcut of a command.  Smile
Reply


Messages In This Thread
Extended KotD #8: _READFILE$ - by SMcNeill - 05-18-2024, 06:17 AM
RE: Extended Kotd #8: _ReadFile$ - by PhilOfPerth - 05-18-2024, 07:24 AM
RE: Extended Kotd #8: _ReadFile$ - by RhoSigma - 05-18-2024, 09:06 AM
RE: Extended Kotd #8: _ReadFile$ - by SMcNeill - 05-18-2024, 09:42 AM
RE: Extended Kotd #8: _ReadFile$ - by RhoSigma - 05-18-2024, 11:08 AM



Users browsing this thread: 1 Guest(s)