Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Overwrite Text File in Loop

Reply
 
Old 08-15-2008   #1 (permalink)
MacMan0295


 
 

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
Old 08-15-2008   #2 (permalink)
krazymike


 
 

Re: Overwrite Text File in Loop

> While source.AtEndOfStream <> True
Quote:

> * * * * 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
Old 08-15-2008   #3 (permalink)
MacMan0295


 
 

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:
Quote:
Quote:

> > 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
Old 08-18-2008   #4 (permalink)
krazymike


 
 

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
Reply

Thread Tools


Similar Threads
Thread Forum
Re: Event Logging and Move File OVERWRITE VB Script
Re: loop through text and create array PowerShell
VS2005 Setup Project - How to prevent overwrite of file? .NET General
How to overwrite a file in Vista Windows folder Vista security
File overwrite Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46