• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 685
  • Last Modified:

VBScript Execution Cycle

I have a script which collects information from a web form and writes the inputs into a .txt file and then forwards the .txt file as the body of an email.

I am concerned as to whether multiple users filling in the form may be able to hit the submit button at the exact same time and run the script simultaneously causing a collision where the user writes over the others text file, thus mixing user information.

I was wondering if anyone knows the order of process for vbscript? Will script wait till it completes one cycle before starting another?

Or will the server lock the .txt file while it is being written too? Will this throw an error?

I am trying to understand the underlying sequence before I write additional code to create uniques text file names and deleting them once I know they have been passed on to the mail server.

Open for discussion....

3 Solutions
Carl TawnSystems and Integration DeveloperCommented:
It may cause you problems. Presumably the files will have to sit around for a good few seconds while they're written to and you get confirmation from the mail server.

As a general rule: If you think it might cause a problem then its best to code as if it was a problem. In this case unique file names would be a good idea.
Here's a function I use sometimes to create filenames for disposable temporary files on the web server.

Function GetRandomFileName()
      Const Source = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
      Dim intLen
      Dim intChar
      Dim strResult
      intLen = Len(Source)
      Randomize Timer
      strResult = ""
      For intChar = 1 To 8
            strResult = strResult & Mid(Source, Int(Rnd(1) * intLen) + 1, 1)
      GetRandomFileName = strResult
End Function

Instead of hard-coding the filename, make the filename a variable (say, strFileName), and do something like:

    strFileName = <pathwherethefilegoes> & GetRandomFileName & ".txt"

Then after you do the attach to the email, delete that file.  Alternatively, you could let them sit around and have the server run a job overnight to delete all the temporary files that were created that day.

Is it necessary that the data be attached to the email as a file at all?  Why can it not be simply the body of the message itself?  If you can just write the information into a string instead of into a file, then you could use the string's contents as the message body, and avoid the file issue altogether.

Hi rockymagee,

Why not just use the session ID for you file name:
  strFilename = <path> & session.sessionID & ".txt"

The session ID is generated by the server when the user connects to the session and is very simple to access.  You code to Session_OnEnd section of the global.asa file to automatically deal with temp files that don't get deleted properly.

Hope this helps,
Independent Software Vendors: 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!

rockymageeAuthor Commented:
Thanks for the responses .... I am going to implement the unique file fix using the session ID as the file name.  


Can you give me a little more explanation on using the global.asa to deal with the temp files ....


In the global.asa file in the Session_OnEnd section add code similar to the following:
set fs = server.createobject("Scripting.FileSystemObject")
vPath = "c:\web\yourdomain\tempfiles\"   'or where ever you are storing your temp files.
vFile = vPath & session.SessionID
if fs.FileExists(vFile) then
end if
set fs = nothing


Keep in mind that the session does not end when the user leaves your site or closes the browser.  The session ends when it times out, or when session.abandon is called.  So the files will not be removed until one of those two things happens.  The default timeout for a session is 20 minutes.  This can be changed at the server level in IIS Admin or within your scripts using session.timeout = xxx

rockymageeAuthor Commented:

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now