Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2377
  • Last Modified:

generate unique file name begins with date mmddyy_

I'm trying to modify a script so that the files it creates and writes to start with the current date (mmddyy), then a unique string, then .txt. Something like this:

112807_5873902901.txt  
or
112807_azbemxpurmp.txt
or
112807_1n5k8c0q9x.txt

Problem is: I don't know how to generate any of the above. Apart from the date at the beginning, I'm not particular about what follows the under bar, just as long as it's unique. Note: The files that are generated won't stick around forever; once processed, they're deleted in a few days.

Here's the code I need to alter. Can someone show me how to create the unique file names? Also, I'd like to set the path in a separate line.

dim fs,tfile
set fs=Server.CreateObject("Scripting.FileSystemObject")
set tfile=fs.CreateTextFile("e:\temp\file1.txt")
tfile.WriteLine(Mail.Body)
tfile.close
set tfileB=nothing
set tfileB=fs.CreateTextFile("e:\temp\file2.txt")
tfileB.WriteLine(Mail.Body)
tfileB.close
set tfileB=nothing
set fs=nothing

Open in new window

0
GessWurker
Asked:
GessWurker
  • 6
  • 2
1 Solution
 
CCongdonCommented:
Try this page from Microsoft.

http://support.microsoft.com/kb/320375

My suggesstion is the 'Create a Simple Random GUID' function. Insert that function into your code, and then call it for creating the second half of your filenames.
0
 
CCongdonCommented:
Oh, and the line that says Form tmpCounter = 1 to 20... the '20' is the length of your random string. Change that number in that line to as little or as much as you need.
0
 
CCongdonCommented:
Bleh. typos. FOR not FORM.
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
GessWurkerAuthor Commented:
CCongdon: I'll need a little more guidance (sorry). I'm a total beginner at asp. I don't even know how to do the date part of the file name (which should be pretty easy, right?)
0
 
CCongdonCommented:
OK here goes. Here is your original code modified....


Function CreateGUID()
  numChars = 10 'CHANGE this variable for the length of the characters after the date
  Randomize Timer
  Dim tmpCounter,tmpGUID
  tmpGUID = Right("0" & Month(Now()),2)
  tmpGUID = Right("0" & tmpGUID & Day(Now()),2)
  tmpGUID = tmpGUID & Right(Year(Now()),2)
  Const strValid = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  For tmpCounter = 1 To numChars
    tmpGUID = tmpGUID & Mid(strValid, Int(Rnd(1) * Len(strValid)) + 1, 1)
  Next
  CreateGUID = tmpGUID
End Function
 
dim fs,tfile
dim filepath, filename
filepath = "e:\temp\" 'Make sure to have a trailing backslash!
set fs=Server.CreateObject("Scripting.FileSystemObject")
filename = filepath & CreateGUID()
set tfile=fs.CreateTextFile(filename)
tfile.WriteLine(Mail.Body)
tfile.close
set tfileB=nothing
filename = filepath & CreateGUID()
set tfile=fs.CreateTextFile(filename)
set tfileB=fs.CreateTextFile(filename)
tfileB.WriteLine(Mail.Body)
tfileB.close
set tfileB=nothing
set fs=nothing

Open in new window

0
 
CCongdonCommented:
I forgot the underscore you wanted seperating the date and random chars.

Find the line that says:

tmpGUID = tmpGUID & Right(Year(Now()),2)

and change it to read:

tmpGUID = tmpGUID & Right(Year(Now()),2) & "_"

0
 
CCongdonCommented:
Bleah. Scratch that completelty. It's going to truncate the date because I got something out of place. Please use this code instead.
Function CreateGUID()
  numChars = 10 'CHANGE this variable for the length of the characters after the date
  Randomize Timer
  Dim tmpCounter,tmpGUID
  tmpGUID = Right("0" & Month(Now()),2)
  tmpGUID = tmpGUID & Right("0" & Day(Now()),2)
  tmpGUID = tmpGUID & Right(Year(Now()),2) & "_"
  Const strValid = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  For tmpCounter = 1 To numChars
    tmpGUID = tmpGUID & Mid(strValid, Int(Rnd(1) * Len(strValid)) + 1, 1)
  Next
  CreateGUID = tmpGUID
End Function
 
dim fs,tfile,filepath,filename
filepath = "e:\temp\" 'Make sure to have a trailing backslash!
set fs=Server.CreateObject("Scripting.FileSystemObject")
filename = filepath & CreateGUID()
set tfile=fs.CreateTextFile(filename)
tfile.WriteLine(Mail.Body)
tfile.close
set tfileB=nothing
filename = filepath & CreateGUID()
set tfile=fs.CreateTextFile(filename)
set tfileB=fs.CreateTextFile(filename)
tfileB.WriteLine(Mail.Body)
tfileB.close
set tfileB=nothing
set fs=nothing

Open in new window

0
 
Mass Dot NetCommented:
Here's my proposed way of doing it, using two custom functions that I typically use to generate GUIDs and format dates.


-= DeathToSpam =-
<%	
Option Explicit
 
'===============================================================================
'// Here's where you actually use the code.
'===============================================================================
Dim sUniqueFileNamePrefix, sUniqueTextFileName
 
'// Here's where most of the heavy lifting is done.
sUniqueFileNamePrefix = CustomFormatDate(Now, "MMddyy") & "_" & GetGUID()
 
'// After you've generated the unique prefix, append the desired file extension.
sUniqueTextFileName = sUniqueFileNamePrefix & ".txt"
Call Response.Write("Custom Text File Name: [<b><font color=""red"">" & sUniqueTextFileName & "</font></b>]<br>" & vbNewLine)
 
 
'###############################################################################
'// Helper function declarations.
'###############################################################################
'===============================================================================
'// GetGUID() - Generate a GUID.
'===============================================================================
Function GetGUID()
	Dim oTypeLib, sGUID
	
	Set oTypeLib = Server.CreateObject("Scriptlet.TypeLib")
		sGUID = Trim(Left(oTypeLib.GUID, 38))
	Set oTypeLib = nothing
	
	GetGUID = sGUID
End Function
 
'===============================================================================
'// Function CustomFormatDate() -- Accepts a Date variant and a date format
'// string, and returns a string representing the date in the desired format.
'===============================================================================
Function CustomFormatDate(sDate, sDateFormat)
	Dim sReturnValue, dtDate, M, MM, MMM, MMMM, d, dd, dddd, yy, yyyy
 
	sReturnValue = ""
	
	if IsDate(sDate) then
		dtDate = cDate(sDate)
		M = Month(dtDate)
		MM = M:  if (M < 10) then MM = "0" & MM
		MMM = Left(MonthName(M), 3)
		MMMM = MonthName(M)
		d = Day(dtDate)
		dd = d:  if (d < 10) then dd = "0" & dd
		dddd = WeekdayName(Weekday(dtDate))
		yy = Right(CStr(Year(dtDate)), 2)
		yyyy = Year(dtDate)
 
		sReturnValue = sDateFormat
		sReturnValue = Replace(sReturnValue, "MMMM", MMMM,1,-1,0)
		sReturnValue = Replace(sReturnValue, "MMM", MMM,1,-1,0)
		sReturnValue = Replace(sReturnValue, "MM", MM,1,-1,0)
		sReturnValue = Replace(sReturnValue, "%M", M,1,-1,0)
		sReturnValue = Replace(sReturnValue, "dddd", dddd,1,-1,0)
		sReturnValue = Replace(sReturnValue, "dd", dd,1,-1,0)
		sReturnValue = Replace(sReturnValue, "%d", d,1,-1,0)
		sReturnValue = Replace(sReturnValue, "yyyy", yyyy,1,-1,0)
		sReturnValue = Replace(sReturnValue, "yy", yy,1,-1,0)
	end if
 
	CustomFormatDate = sReturnValue
End Function
%>

Open in new window

0
 
GessWurkerAuthor Commented:
CCongdon: Thanks! I got your solution working - with very slight adjustments. See code below.

Function CreateGUID()
  numChars = 10 'CHANGE this variable for the length of the characters after the date
  Randomize Timer
  Dim tmpCounter,tmpGUID
  tmpGUID = Right("0" & Month(Now()),2)
  tmpGUID = tmpGUID & Right("0" & Day(Now()),2)
  tmpGUID = tmpGUID & Right(Year(Now()),2) & "_"
  Const strValid = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  For tmpCounter = 1 To numChars
    tmpGUID = tmpGUID & Mid(strValid, Int(Rnd(1) * Len(strValid)) + 1, 1)
  Next
  CreateGUID = tmpGUID
End Function
 
dim fs,tfile,filepath,filename
filepath = "e:\temp\" 'Make sure to have a trailing backslash!
set fs=Server.CreateObject("Scripting.FileSystemObject")
filename = filepath & CreateGUID() & ".txt" 'need an extension
set tfile=fs.CreateTextFile(filename)
tfile.WriteLine(Mail.Body)
tfile.close
'set tfileB=nothing  'had to comment this out, cause problems
filename = filepath & CreateGUID() & ".txt" 'need an extension
'set tfile=fs.CreateTextFile(filename) 'commented out
set tfileB=fs.CreateTextFile(filename) 
tfileB.WriteLine(Mail.Body)
tfileB.close
set tfileB=nothing
set fs=nothing 

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now