Solved

generate unique file name begins with date mmddyy_

Posted on 2007-11-28
9
2,372 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

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…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

717 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