| If you've ever used Word, or any other Office application, you probably noticed that each time you open a file, Office creates a temporary file to store changes. You may have wondered how to generate random temporary file names in your own Visual Basic application. To do so, use the GetTempFileName API function, which you declare in a standard module, like so Public Declare Function GetTempFileName Lib "kernel32" _ Alias "GetTempFileNameA" (ByVal lpszPath As String, _ ByVal lpPrefixString As String, ByVal wUnique As Long, _ ByVal lpTempFileName As String) As Long Pass the full path name in the lpszPath argument. The lpPrefixString lets you add a three letter prefix to the beginning of the filename, and wUnique tells Windows to either create a random file name (0 setting) or use the number you supply. The lpTempFileName, of course, contains the new temporary filename. As an example, place the API declaration above in a standard module, then add the following function Private Function GenTempName(sPath As String) Dim sPrefix As String Dim lUnique As Long Dim sTempFileName As String If IsEmpty(sPath) Then sPath = "D:\Articles\IVB" sPrefix = "fVB" lUnique = 0 sTempFileName = Space$(100) GetTempFileName sPath, sPrefix, lUnique, sTempFileName sTempFileName = Mid$(sTempFileName, 1, InStr(sTempFileName, Chr$(0)) - 1) GenTempName = sTempFileName End Function Now, open a new form and add the following code to its Click() event. (Replace D:\Articles\IVB with any valid path) MsgBox GenTempName("D:\Articles\IVB") |
Generate temporary Visual Basic files with API |
Express News India | Freelance ecommerce web development India