?
Solved

generate unique file name begins with date mmddyy_

Posted on 2007-11-28
9
Medium Priority
?
2,376 Views
Last Modified: 2012-05-05
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
Comment
Question by:GessWurker
  • 6
  • 2
9 Comments
 
LVL 9

Expert Comment

by:CCongdon
ID: 20365625
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
 
LVL 9

Expert Comment

by:CCongdon
ID: 20365639
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
 
LVL 9

Expert Comment

by:CCongdon
ID: 20365640
Bleh. typos. FOR not FORM.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

Author Comment

by:GessWurker
ID: 20365666
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
 
LVL 9

Expert Comment

by:CCongdon
ID: 20365806
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
 
LVL 9

Expert Comment

by:CCongdon
ID: 20365825
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
 
LVL 9

Accepted Solution

by:
CCongdon earned 2000 total points
ID: 20365849
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
 
LVL 9

Expert Comment

by:Mass Dot Net
ID: 20367201
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
 

Author Comment

by:GessWurker
ID: 20375248
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
There is a wide range of advantages associated with the use of ASP.NET. This is why this programming framework is used to create excellent enterprise-class websites, technologies, and web applications.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Hi, this video explains a free download that you can incorporate into your Access databases, or use stand-alone for contact management. Contacts -- Names, Addresses, Phone Numbers, eMail Addresses, Websites, Lists, Projects, Notes, Attachments…
Suggested Courses

593 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question