?
Solved

generate unique file name begins with date mmddyy_

Posted on 2007-11-28
9
Medium Priority
?
2,373 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

765 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