Windows Vista Forums

Overwrite Text File in Loop
  1. #1


    MacMan0295 Guest

    Overwrite Text File in Loop

    Here is some code that I am writing that will pull from a text file some
    options to use running a command prompt program. As it loops through the
    list, every time it needs to overwrite a text file.

    I get permission denied with the code as is below. If I try to thow a wait
    command in, I get an error from the application that the body.txt cannot be
    read. Please let me know what I am doing wrong. Thanks

    <code>
    Const ForReading = 1
    Const ForWriting = 2

    strInput = "options.txt"



    set input = WScript.StdIn
    set output = WScript.StdOut

    set wshobj = WScript.CreateObject("WScript.Shell")
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set source = fso.OpenTextFile(strInput, ForReading)

    While source.AtEndOfStream <> True
    Set srcBody = fso.CreateTextFile("body.txt", ForWriting)
    strOptions = source.Readline
    strOptionsArray = split(strOptions,",")
    emailUsers strOptionsArray(0),strOptionsArray(1)
    Wend
    source.close

    Function emailUsers(strShareName,strEmailUsers)
    srcBody.writeline "[Body Message]"
    strCmd = "blat body.txt -to " & strEmailUsers & " -s ""** " & strShareName
    & " Share Verification **"" -f user -server smtp.atmel.com -log log.txt
    -timestamp"
    WScript.Echo strCmd
    objRun = wshobj.Run(strCmd)
    End Function

    <code>

      My System SpecsSystem Spec

  2. #2


    krazymike Guest

    Re: Overwrite Text File in Loop

    > While source.AtEndOfStream <> True

    > * * * * Set srcBody = fso.CreateTextFile("body.txt", ForWriting)
    > * * * * strOptions = source.Readline
    > * * * * strOptionsArray = split(strOptions,",")
    > * * * * emailUsers strOptionsArray(0),strOptionsArray(1)
    > Wend
    Simply move your CreateTextFile line above the loop and I changed

    objRun = wshobj.Run(strCmd)

    to

    srcBody.writeline strCmd

    Also, remember to close your files when you're through with them.
    srcBody.Close source.Close

      My System SpecsSystem Spec

  3. #3


    MacMan0295 Guest

    Re: Overwrite Text File in Loop

    Changing that line just writes the command I want to execute to the file
    rather than executing it.

    "krazymike" wrote:

    > > While source.AtEndOfStream <> True
    > > Set srcBody = fso.CreateTextFile("body.txt", ForWriting)
    > > strOptions = source.Readline
    > > strOptionsArray = split(strOptions,",")
    > > emailUsers strOptionsArray(0),strOptionsArray(1)
    > > Wend
    >
    > Simply move your CreateTextFile line above the loop and I changed
    >
    > objRun = wshobj.Run(strCmd)
    >
    > to
    >
    > srcBody.writeline strCmd
    >
    > Also, remember to close your files when you're through with them.
    > srcBody.Close source.Close
    >

      My System SpecsSystem Spec

  4. #4


    krazymike Guest

    Re: Overwrite Text File in Loop

    Sorry, I didn't look at that too well, did I? You can still only
    create the file once. You can use a FileStream object to read the
    contents of a file into a string variable. Problem is, you may have
    trouble reading it. This overwrites body.txt. You have to close and
    reopen it. Try this out, let me know.

    Const ForReading = 1
    Const ForWriting = 2
    Const ForAppending = 8 'another opening mode. Doesn't overwrite.

    strInput = "options.txt"

    set input = WScript.StdIn
    set output = WScript.StdOut

    set wshobj = WScript.CreateObject("WScript.Shell")
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set source = fso.OpenTextFile(strInput, ForReading)

    While source.AtEndOfStream <> True
    Set srcBody = fso.CreateTextFile("body.txt",ForWriting)
    strOptions = source.Readline
    strOptionsArray = split(strOptions,",")
    emailUsers strOptionsArray(0),strOptionsArray(1)
    Wend
    source.close
    srcBody.close

    Function emailUsers(strShareName,strEmailUsers)
    srcBody.writeline "[Body Message]"
    dim fs, body
    srcBody.close
    set srcBody = Nothing
    set fs = fso.opentextfile("body.txt", forreading)
    body = fs.readall
    strCmd = "blat " & body & " -to " & strEmailUsers & " -s ""** " &
    strShareName & " Share Verification **"" -f user -server
    smtp.atmel.com -log log.txt -timestamp"
    WScript.Echo strCmd
    objRun = wshobj.Run(strCmd)
    End Function

      My System SpecsSystem Spec

Overwrite Text File in Loop problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
for ... loop through file collection instead of for each ... loop Ranah van Rijswijk VB Script 2 21 Feb 2010
Re: Event Logging and Move File OVERWRITE Larry Serflaten VB Script 1 24 Jul 2009
VS2005 Setup Project - How to prevent overwrite of file? flutophilus .NET General 1 03 Mar 2008
How to overwrite a file in Vista Windows folder Les Vista security 12 06 Nov 2007
File overwrite =?Utf-8?B?U3Rvb3A=?= Vista General 1 14 Sep 2006