VBScript Execution Cycle

Posted on 2006-03-20
Last Modified: 2008-03-03
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....

Question by:rockymagee
    LVL 52

    Assisted Solution

    by:Carl Tawn
    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.
    LVL 13

    Assisted Solution

    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.

    LVL 19

    Expert Comment

    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,
    LVL 6

    Author Comment

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

    LVL 19

    Accepted Solution


    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

    LVL 6

    Author Comment


    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    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…
    I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    734 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

    21 Experts available now in Live!

    Get 1:1 Help Now