sFullName = String$(cMaxPath, 0)
Call GetTempFileName(“.”, “HC”, 0, sFullName)
sFullName = Left$(sFullName, InStr(sFullName, sNullChr) - 1)
fExist = (Dir$(sFullPath) <> sEmpty)
fExist = FileLen(sFullPath)
Function ExistFile(sSpec As String) As Boolean
On Error Resume Next
Call FileLen(sSpec)
ExistFile = (Err = 0)
End Function
Function ExistFileDir(sSpec As String) As Boolean
Dim af As Long
af = GetFileAttributes(sSpec)
ExistFileDir = (af <> -1)
End Function
I didn’t think there would be any way to break this one, but it turns out that certain filenames containing control characters are legal on Windows 95 but illegal on Windows NT. Or is it the other way around? Anyway, I have seen this function fail in situations too obscure to describe here.
I hate to waste brain cells on a function so trivial. But 99 percent effective existence tests aren’t good enough on a disk containing thousands of files.
‘ Get temp file for current directory
sFullName = GetTempFile(“VB”, “.”)
‘ Get temp file for TEMP directory
sFullName = GetTempFile(“VB”, GetTempDir)
‘ Get temp file for TEMP directory default
sFullName = GetTempFile(“VB”)
‘ Get temp file for TEMP directory with no prefix
sFullName = GetTempFile
The calls to GetTempFile in the samples above generated C:\CURDIR\VB4B.TMP, C:\TEMP\VB4C.TMP, C:\TEMP\VB4D.TMP, and C:\TEMP\4E.TMP. The filenames vary on different operating systems and different file systems. The only guarantee is that the generated file doesn’t currently exist. Windows will keep incrementing the number and trying the file until it finds a unique name. It’s up to you to open the file, process it, and—unless you want to be considered the crudest and most illiterate of programmers—delete it when you’re done.
This flame is directed at myself—in other words, it’s an apology for a crime I hope you will never commit (and that I will never repeat). I changed the interface of GetFullPath, SearchDirs, and GetTempFile. These functions did not use optional parameters in the C DLL versions described in the first edition of this book. Later I wrote a series of articles published on the Internet and in MSDN (also provided on the CD of this book) that described a different C++ version of the VBUTIL DLL. In this version, the functions used optional parameters—and that changed the design. The original parameter order matched the order of the API functions—a design decision that showed a lack of imagination. I “fixed” the order problem in the second version. The second DLL version was targeted at Visual Basic 4, which supported optional parameters only through Variants. But when I rewrote the functions in Basic for this edition, I changed to typed optional parame- ters. The bottom line is that if you’ve been a faithful follower of my writing, your code has been broken twice by incompatible versions of my functions. Fortunately, the interfaces of the wrapper functions—GetFullPath, GetFileBase, GetFileBaseExt, GetFileExt, and GetFileDir—haven’t changed, although their implemen- tation has. The moral of the story is: get your design right the first time. This is a good rule for all code, and it’s the law for ActiveX components. Since I’ve published VBCore as a component, I’d have to write new functions rather than change the old ones if I came up with more bright ideas.