Create text file vbs

Posted on 2012-09-18
Last Modified: 2012-09-18
Our Accounts package has some basic vb scripting functionality.

I have created a function that works when a purchase receipt is posted, it emails users if a parameter is true.

I only want to send one message per order so I thought I would create a text file in C:\Temp.
e.g 'Ordernumber.txt'. On subsequent receipts I can check if the file exists to prevent duplicate emails being sent.

Can anyone help with the syntax?
Question by:HKFuey
    LVL 7

    Accepted Solution

    Example VBS code:

    Sub CreateAfile
       Dim fso, MyFile
       Set fso = CreateObject("Scripting.FileSystemObject")
       Set MyFile = fso.CreateTextFile("c:\testfile.txt", True)
       MyFile.WriteLine("This is a test.")
    End Sub
    If fso.FileExists(path) Then
       Response.Write ("Your file, '" & getname & "', has been created.")
    End If

    Open in new window


    Author Comment

    OK, I'm nearly there, I have created the text file but the part that checks if it exits crashes on line 6 with "Error number 424 Object required: 'fso'

    Dim SavFil 
    Dim fso, MyFile
    SavFil = "C:\Temp\" & PurchaseOrder & ".txt"
    msgbox SavFil
    If fso.FileExists(SavFil) Then
       msgbox SavFil & " Already Created"
    exit sub
    End If
    'Create file to flag order
       Set fso = CreateObject("Scripting.FileSystemObject")
       Set MyFile = fso.CreateTextFile(SavFil, True)
       MyFile.WriteLine("This is a test.")

    Open in new window


    Author Closing Comment

    Needed to move  Set fso = CreateObject("Scripting.FileSystemObject") further up in my code!!

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Unlike scripting languages such as C# where a semi-colon is used to indicate the end of a command, Microsoft's VBScript language relies on line breaks to determine when a command begins and ends. As you can imagine, this quickly results in messy cod…
    Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    754 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