Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Beginner
Aug 6, 1997



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Create Temporary Files

I'm developing a database program that deals with many auxiliary files at same time. Everyone coding database programs must create some temporary files to produce an output from SQL or a temporary database to manipulate those records efficiently. I decided to create a FileAux function that returns one temporary file name. If I need to create more than one file into the same process, I simply store those names into variables I dimensioned before:
Function FileAux(Ext As String) _
        As String
        Dim i As Long, X As String
        If InStr(Ext, ".") = 0 Then
                Ext = "." + Ext
        End If

        'Look for the previous files in the 
        i = 0
                X = "Aux" + Format$(i, "0000") _
                        + Ext
                If FileExists(X) Then
                        i = i + 1
                        Exit Do
                End If
        FileAux = X
End Function
This function uses the "FileExists" function:
Function FileExist(filename As String) _
        As Boolean
        FileExist = Dir$(filename) <> ""
End Function
Here's an example of its usage:
Sub Test()
        Dim File1 As String, File2 As _
                String, File3 As String
        Dim DB1 As database, DB2 As DataBase
        Dim FileNum As Integer
        File1 = FileAux("MDB")
        Set DB1 = CreateDataBase(File1)
        File2 = FileAux("MDB")
        Set DB2 = CreateDataBase(File2)
        File3 = FileAux("TXT")
        FileNum = FreeFile
        Open File3 For OutPut As FileNum
        'Your code
        ' ...
        Close FileNum
End Sub
File1, File2, and File3 should be "Aux0001.MDB," "Aux0002.MDB," and "Aux0001.TXT," respectively.
Luis Orlindo
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date