Solved

generate unique file name begins with date mmddyy_

Posted on 2007-11-28
9
2,365 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 500 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:deathtospam
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

759 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now