![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||