Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random generator for file 1

Status
Not open for further replies.

rds747

Technical User
Mar 8, 2005
180
US
What is the best way to create a random number for a file? For example, I would like the file name to be "C:\Test42356.tif". The file is a temp file which will be later deleted from the drive.

From MSDN I could try this:

MyValue = Int((100000 * Rnd) + 1)
' Generate random number between 1 and 100001.

Just wanted your advice.

Thanks!

 
Use GetTempName (FileSystemObject).

From MSDN:
Code:
GetTempName Method 

Returns a randomly generated temporary file or folder name that is useful for performing operations that require a temporary file or folder.

   object.GetTempName ( ) 

The optional object is always the name of a FileSystemObject. 

The GetTempName method does not create a file. It provides only a temporary file name that can be used with CreateTextFile to create a file.

The following example illustrates the use of the GetTempName method.


Dim fso, tempfile
Set fso = CreateObject("Scripting.FileSystemObject")

Set tempfile = CreateTempFile
tempfile.WriteLine "Hello World"
tempfile.Close

Function CreateTempFile 
   Dim tfolder, tname, tfile

   Const TemporaryFolder = 2
   Set tfolder = fso.GetSpecialFolder(TemporaryFolder)
   tname = fso.GetTempName    
   Set tfile = tfolder.CreateTextFile(tname)
   Set CreateTempFile = tfile

End Function
 
Or you could use the current date/time stamp:

Dim dtmNow As Date

dtmNow = Now
MsgBox Year(dtmNow) & Month(dtmNow) & Day(dtmNow) & Hour(dtmNow) & Minute(dtmNow) & Second(dtmNow)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top