Skip to content

Add A Logfile To Your VBScript

Today I’m going to share a little bit of code that I wrote. It’s nothing fancy, so if you’re accustomed to writing VBScript this most likely wont help you.

Put this at the top of your own scripts to add a function to write all debug comments to a log. I found myself needing this when my scripts were going to be run en masse and outputting comments to the terminal wasn’t going to cut it.


  • Set boolDebug to True to turn on debugging. Alternatively, call /DEBUG from the command line to turn on debugging (and override boolDebug)
  • Set strDebugDir and strDebugFile to a location the script has write privileges to.
  • To use it, call the Debug() function with whatever you want written to the file. i.e. Debug(“Text message goes here!”) or Debug(“Error: ” & strErr)
boolDebug = true
strDebugDir = "C:\Debug" 'What folder will your log be in?
strDebugFile = "\logfile.txt" 'What is the name of your logfile?

On Error Resume Next
'Handle user variables
intCount = WScript.Arguments.Count - 1 ' reset arguments to proper counting
for i = 0 to intCount
	strArgument = UCase(WScript.Arguments(i)) ' to uppercase
	if strArgument = "/DEBUG" then
		boolDebug = true
	elseif strArgument = "/NODEBUG" then
		boolDebug = false
	end if

'Setup Debugging
if boolDebug = true then
	set objFSO = CreateObject("Scripting.FileSystemObject")
	if not objFSO.FolderExists(strDebugDir) then
		set objDebugDir = objFSO.CreateFolder(strDebugDir)
	end if

	if not objFSO.FileExists(strDebugDir & strDebugFile) then
		set objDebugFile = objFSO.CreateTextFile(strDebugDir & strDebugFile)
	end if

	set objDebugDir = nothing
	set objDebugFile = nothing

	strDateStamp = Now()
end if

'Handle all debugging
sub Debug( strLog )
	if boolDebug = true then
		set objTextFile = objFSO.OpenTextFile(strDebugDir & strDebugFile, 8, true)
		objTextFile.WriteLine( strDateStamp & ": " & strLog )
	end if
end sub

Enjoy! I always appreciate feedback and credit, but it’s not required.

Posted in Tech, Windows.

Tagged with , , , , , , , , , , .

0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

Some HTML is OK

or, reply to this post via trackback.