VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Creates a completely random file name. (Use as a temporary filespec for ex.) Currently generates MS

by Gareth Lock (DynaByte Software) (3 Submissions)
Category: Files/File Controls/Input/Output
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Sun 20th April 2003
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Creates a completely random file name. (Use as a temporary filespec for ex.) Currently generates MS-DOS 8+3 style filenames using capital A-Z

API Declarations


'Purpose :- Create a random filename.
'Version :- 1.00.0000
'Object :- NameGen (Module)

Option Explicit

Rate Creates a completely random file name. (Use as a temporary filespec for ex.) Currently generates MS



' Creates a completely random file name. (Use as a temporary filespec for ex.)
' Currently generates MS-DOS 8+3 style filenames using capital A-Z only. That
' should be enough for most applications when used with "*".
' **************************************************************************
' ** namegen([Extension]) : Returns a random filename.  **
' ** [Extension] : Allows you to specify an **
' **extension for the file. If  **
' **this is not specified .TMP  **
' **is used. Use * to randomize **
' **the extension.  **
' **************************************************************************
Public Function namegen(Optional Extension As Variant)
 Dim i As Integer, filespec As String, c As Integer, ch As String'Declare Variables.
 Dim upperbound As Integer, lowerbound As Integer
 Randomize 'Init rnd.
 upperbound = 90 'Set char limits.
 lowerbound = 65
 
 If IsMissing(Extension) Then  'Do we add .TMP?
  Extension = ".TMP"'Add ".TMP".
 Else
  If Extension = "*" Then'Randomize ext.
Extension = ""'Remove *.
For i = 1 To 3
 c = Int((upperbound - lowerbound + 1) * Rnd + lowerbound) 'Pick random ASCII.
 ch = Chr(c)  'Convert to char.
 Extension = Extension & ch  'Append to Extension.
Next
  End If
  Extension = "." & Extension'Add the "." to start.
 End If

 For i = 1 To 8 'Create 8 char f/n.
  c = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)'Pick random ASCII.
  ch = Chr(c) 'Convert to char.
  filespec = filespec & ch'Append to f/n.
 Next
 filespec = filespec & Extension'Add extension.
 namegen = filespec 'Return filename.
End Function

Download this snippet    Add to My Saved Code

Creates a completely random file name. (Use as a temporary filespec for ex.) Currently generates MS Comments

No comments have been posted about Creates a completely random file name. (Use as a temporary filespec for ex.) Currently generates MS. Why not be the first to post a comment about Creates a completely random file name. (Use as a temporary filespec for ex.) Currently generates MS.

Post your comment

Subject:
Message:
0/1000 characters