Write to a fileTag(s): WSH VBScript


[writefile.vbs]
Dim objFileSystem, objOutputFile
Dim strOutputFile

' generate a filename base on the script name
strOutputFile = "./" & Split(WScript.ScriptName, ".")(0) & ".out"

Set objFileSystem = CreateObject("Scripting.fileSystemObject")
Set objOutputFile = objFileSystem.CreateTextFile(strOutputFile, TRUE)

objOutputFile.WriteLine("Hello world (" & Now & ")")
objOutputFile.Close

Set objFileSystem = Nothing

WScript.Quit(0)
To append to an existing file :

[appendfile.vbs]


Dim objFileSystem, objOutputFile
Dim strOutputFile

Const OPEN_FILE_FOR_APPENDING = 8

' generate a filename base on the script name
strOutputFile = "./writefile.out"

Set objFileSystem = CreateObject("Scripting.fileSystemObject")
Set objOutputFile = objFileSystem.OpenTextFile(strOutputFile, _
  OPEN_FILE_FOR_APPENDING)

objOutputFile.WriteLine("Hello world (" & Now & ")")
objOutputFile.Close

Set objFileSystem = Nothing

WScript.Quit(0)

A simple logging function

dim loggit_logfilename, loggit_fso, loggit_silent

loggit_logfilename = ".\exec.log"
loggit_silent = true   ' log file only or with MsgBox/Echo
set loggit_fso = CreateObject("Scripting.FileSystemObject")


loggit "Started..."

loggit "Done."

' -=-=-=-=-=-=-=-=-=-
sub loggit (msg)
    set stream = loggit_fso.OpenTextFile(loggit_logfilename, 8, True)
    stream.writeline date & " " & time & ": " & msg
    stream.close
    if not loggit_silent then
       WScript.echo msg 
    end if   
end sub
Output (exec.log)
2007-07-10 00:09:29: Started...
2007-07-10 00:09:29: Done.

blog comments powered by Disqus