Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBScript number "incrementor"

Status
Not open for further replies.

jasonsalas

IS-IT--Management
Jun 20, 2001
480
GU
Hi everyone,

I'm looking to find help with writing a function/variables/whatever that will automatically increment a numeric value in a variable by a factor of "1" each time it is fired, without ever repeating that number, for purposes of dyamically-generating unique ASP filesnames on my server.

To make this clear as mud, I want to insert some sort of operation on a page which will launch when a form is submitted (maybe as a hidden field?), so that the result after using it 4 times will be like so:

1224.asp
1225.asp
1226.asp
1227.asp

Anyone got any ideas?

Thanks!

Jas
 
Good question, my VBSCript manual does not mention it at all! It is not listed under operators, or increment. Surely there must be one, you could store it in db though if you had to.
 
Yeah...that's the thing...I'm doing it as a system which will generate filename for a series of XML pages...so I'm trying to avoid the DB solution altogether.

I was thinking of using a date/timestamp as the basis, since theoretically, that will never have the same exact value more than once....but I dunno.

I actually got the idea from MSNBC.COM, who I work with as an affilate site. They do something like this, and I'm trying to reverse engineer it to figure out how to make it work for me.
 
Well, I basically gave it my own best shot and came out with the following. I combined the use of the VBScript CStr and NOW functions (which because 'Now()' displays that moment's time/datestamp, should theoretically always give out unique values), and wrote that line as a string.

To avoid the slight risk of running out of numeric values, I added an alphabetical character ("M") at the beginning of the filename, so in the event I ever do run out of numbers, I can just change the script to print out "J" instead of an "M".

Here's the snippet I was talking about:

' CREATE A UNIQUE FILENAME
Dim timeStamp, strFileName, savedFileName
timestamp = FormatNumber(Now(),0,0,0,0)
strFileName = CStr(timeStamp)
savedFileName = "M" & strFileName & ".xml"
Response.Write "Congrats, genius! You have successfully created the string: " & savedFileName

...and here's the total script to render an XML document with an XSL stylesheet.


'*****************************************************
' Number incrementor script
' ****************************************************

<%
' CREATE A UNIQUE FILENAME
Dim timeStamp, strFileName, savedFileName
timestamp = FormatNumber(Now(),0,0,0,0)
strFileName = CStr(timeStamp)
savedFileName = &quot;M&quot; & strFileName & &quot;.xml&quot;

' Transforms an XML document using XSLT, using the MSXML DOM
' Set the source document and stylesheet locations on the server
sourceFile = Request.ServerVariables(&quot;APPL_PHYSICAL_PATH&quot;) + &quot;\M37115.xml&quot;
styleFile = Request.ServerVariables(&quot;APPL_PHYSICAL_PATH&quot;) + &quot;\newspaper.xsl&quot;

' Load the XML document
Set source = Server.CreateObject(&quot;Microsoft.XMLDOM&quot;)
source.async = false
source.load(sourceFile)

' Load the XSL stylesheet
Set style = Server.CreateObject(&quot;Microsoft.XMLDOM&quot;)
style.async = false
style.load(styleFile)

HTMLCode = source.transformNode(style)


' CREATE A NEW TEXTFILE ON THE SERVER
Set objFS = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set objFile = objFS.CreateTextFile(Request.ServerVariables(&quot;APPL_PHYSICAL_PATH&quot;) + &quot;\M&quot; & strFileName & &quot;.xml&quot;)
objFile.Write(HTMLCode)

Response.Write &quot;Your file has been successfully written to disk as: &quot; & savedFileName
Set objFS = Nothing

%>
 
Also, have you tried using server side JScript, and using JScript objects, this way the increment operator is available - sorry I only found out JScript is server runnable today [you probably knew that]:

// make sure this line is first on page, with your
// buffer setting, otherwise the script is executed as a
// header. Try leaving it out, you'll see what happens.
<%@ language=&quot;JScript&quot; %>
...

<script language=&quot;JScript&quot; RUNAT=server>
...
var someNumber = 5;
for(var i=0;i<someNumber;i++){
Response.write(&quot;file&quot;+i)
}
...
</script>


might save you some aggravation! There is more on this here.

;-)
 
Have you though about using an application variable in the global.asa of your site that gets incrimented everytime someone enters your site.

Something to the effect of
Application_onStart
Application(&quot;var&quot;)=somenumber

Sub Session_onStart
Application(&quot;var&quot;) = Application(&quot;var&quot;)+1
end sub

I think that should give you what you are looking for if I understood you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top