Tuesday, June 20, 2006

Generate a Random 8.3 Filename With VBScript, and Verify it Doesn't Already Exist

This is a pretty handy set of functions. In lots of the other scripts I have published, I have hard coded in a temporary working file name to copy our data into while we are trying to process data. While this works fine and dandy when there is just one copy of the script running, if you are calling a function recursively or running more than one copy of the script at a time, you will run into errors because the scripts will step on top of each other trying to copy data into, and out of the same working file.

This is kind of a bummer that wasn't immediately foreseen. Well, that's an easy fix too. All we have to do to resolve that is write yet another function to generate a random file name, and one more to make sure that the file name we create doesn't already exist in our temporary working directory.

We can do this a variety of ways but for the sake of compactness and neatness we can loop through the ASCII character numbers so we don't have to list every single character out and associate them with the random number that is created. The first function, "RndFileName" will generate the random 8.3 file name no questions asked. Quick and dirty.

The second function, "TempFile" calls the first function, generating the file name, but also checks our %TEMP% folder to ensure that this file doesn't already exist. If it does, it re-calls the "RndFileName" function in a do-loop until it finds a filename that isn't in use, then returns this file name as it's value.

Here is the code:

Function RndFileName()
  'This function will generate an alpha-numeric random 8.3
  'file name that ends in .tmp. This function does not accept
  'any arguments.
  'This script is provided under the Creative Commons license located
  'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not
  'be used for commercial purposes with out the expressed written consent
  'of NateRice.com

 
  For x=1 To 8
    Randomize
    vChar = Int(36*Rnd)
    If vChar < 10 Then 'append number
      RndFileName = RndFileName & vChar
    Else 'else append a letter
      RndFileName = RndFileName & Chr(97+(vChar-10))
    End If
  Next

  RndFileName = RndFileName & ".tmp"

End Function

Function TempFile()
  'We'll use this function to generate a working temp file name
  'that we can reliably copy data into and out of while
  'we're generating data. We don't actually create the file
  'here we just generate the name and verify that a file of
  'this name doesn't already exist. We can use this function to
  'generate an 8.3 temporary random file name so that we can
  'call our other functions recursively or run 2 scripts at the
  'same time without encountering weird errors. This function
  'does not accept any arguemtents.
  'This script is provided under the Creative Commons liscense located
  'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not
  'be used for comercial purposes with out the expressed written consent
  'of NateRice.com
  
  Const OpenAsDefault = -2
  Const FailIfNotExist = 0
  Const ForReading = 1
  
  Set oShell = CreateObject("WScript.Shell")
  Set oFSO = CreateObject("Scripting.FileSystemObject")
  sTemp = oShell.ExpandEnvironmentStrings("%TEMP%")
  
  Do Until sGenerationIsSuccessful
    sWorkingFileName = RndFileName
    If oFSO.FileExists(sTemp & "\" & sWorkingFile) Then
      'File exists loop again
      sGenerationIsSuccessful = False
    Else
      'File Doesn't exist and is safe to use
      sGenerationIsSuccessful = True
    End If
  Loop
  
  TempFile = sWorkingFileName

  Set oShell = Nothing
  Set oFSO = Nothing
End Function


That's all for now. Questions and comment's, as always, are welcome.
Send this to:                          

Comments

Mike said...

Can this be used to change the names of a series of files to random ones?

8/12/2006 5:32:15 PM

Nate said...

You could easily use this to rename the files in a particular directory. Generate a filename with these functions and then do a "For Each" loop in the directory of your choice. Feel free to email me if you need some help getting it working for you.

8/13/2006 10:36:12 AM

Ammo said...

You could easily use this to rename the files in a particular directory. Generate a filename with these functions and then do a "For Each" loop in the directory of your choice. Feel free to email me if you need some help getting it working for you.

I would like to change the names of the files in a directory to random file names could you help with this?

1/5/2008 5:37:28 AM

said...

What''s wrong with oFSO.GetTempName ?

4/20/2009 12:23:29 AM

Name
URL
Email
Email address is not published
Remember Me
Comments

CAPTCHA
Write the characters in the image above