{"id":489,"date":"2011-03-22T14:57:57","date_gmt":"2011-03-22T22:57:57","guid":{"rendered":"http:\/\/www.chaotickingdoms.com\/blog\/?p=489"},"modified":"2011-03-22T14:57:57","modified_gmt":"2011-03-22T22:57:57","slug":"add-a-logfile-to-your-vbscript","status":"publish","type":"post","link":"http:\/\/www.chaotickingdoms.com\/blog\/2011\/03\/add-a-logfile-to-your-vbscript\/","title":{"rendered":"Add A Logfile To Your VBScript"},"content":{"rendered":"<p>Today I&#8217;m going to share a little bit of code that I wrote. It&#8217;s nothing fancy, so if you&#8217;re accustomed to writing VBScript this most likely wont help you.<\/p>\n<p>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&#8217;t going to cut it.<\/p>\n<p>NOTES:<\/p>\n<ul>\n<li>Set boolDebug to True to turn on debugging. Alternatively, call \/DEBUG from the command line to turn on debugging (and override boolDebug)<\/li>\n<li>Set strDebugDir and strDebugFile to a location the script has write privileges to.<\/li>\n<li>To use it, call the Debug() function with whatever you want written to the file. i.e. Debug(&#8220;Text message goes here!&#8221;) or Debug(&#8220;Error: &#8221; &amp; strErr)<\/li>\n<\/ul>\n<pre>''' BEGIN USER DEFINED VARIABLES'''\r\nboolDebug = true\r\nstrDebugDir = \"C:\\Debug\" 'What folder will your log be in?\r\nstrDebugFile = \"\\logfile.txt\" 'What is the name of your logfile?\r\n''' END USER DEFINED VARIABLES '''\r\n\r\nOn Error Resume Next\r\n'Handle user variables\r\nintCount = WScript.Arguments.Count - 1 ' reset arguments to proper counting\r\nfor i = 0 to intCount\r\n\tstrArgument = UCase(WScript.Arguments(i)) ' to uppercase\r\n\tif strArgument = \"\/DEBUG\" then\r\n\t\tboolDebug = true\r\n\telseif strArgument = \"\/NODEBUG\" then\r\n\t\tboolDebug = false\r\n\tend if\r\nnext\r\n\r\n'Setup Debugging\r\nif boolDebug = true then\r\n\tset objFSO = CreateObject(\"Scripting.FileSystemObject\")\r\n\tif not objFSO.FolderExists(strDebugDir) then\r\n\t\tset objDebugDir = objFSO.CreateFolder(strDebugDir)\r\n\tend if\r\n\r\n\tif not objFSO.FileExists(strDebugDir &amp; strDebugFile) then\r\n\t\tset objDebugFile = objFSO.CreateTextFile(strDebugDir &amp; strDebugFile)\r\n\tend if\r\n\r\n\tset objDebugDir = nothing\r\n\tset objDebugFile = nothing\r\n\r\n\tstrDateStamp = Now()\r\nend if\r\n\r\n'Handle all debugging\r\nsub Debug( strLog )\r\n\tif boolDebug = true then\r\n\t\tset objTextFile = objFSO.OpenTextFile(strDebugDir &amp; strDebugFile, 8, true)\r\n\t\tobjTextFile.WriteLine( strDateStamp &amp; \": \" &amp; strLog )\r\n\t\tobjTextFile.Close()\r\n\tend if\r\nend sub<\/pre>\n<p>Enjoy! I always appreciate feedback and credit, but it&#8217;s not required.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to log comments to a separate file in your VBScripts. Just copy+paste this code in and start using it!<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,17],"tags":[236,232,235,233,203,270,62,237,234,238,274],"class_list":["post-489","post","type-post","status-publish","format-standard","hentry","category-tech","category-windows","tag-code","tag-debug","tag-howto","tag-log","tag-programming","tag-tech","tag-tutorial","tag-vb","tag-vbscript","tag-visual-basic","tag-windows"],"_links":{"self":[{"href":"http:\/\/www.chaotickingdoms.com\/blog\/wp-json\/wp\/v2\/posts\/489","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.chaotickingdoms.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.chaotickingdoms.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.chaotickingdoms.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.chaotickingdoms.com\/blog\/wp-json\/wp\/v2\/comments?post=489"}],"version-history":[{"count":3,"href":"http:\/\/www.chaotickingdoms.com\/blog\/wp-json\/wp\/v2\/posts\/489\/revisions"}],"predecessor-version":[{"id":492,"href":"http:\/\/www.chaotickingdoms.com\/blog\/wp-json\/wp\/v2\/posts\/489\/revisions\/492"}],"wp:attachment":[{"href":"http:\/\/www.chaotickingdoms.com\/blog\/wp-json\/wp\/v2\/media?parent=489"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.chaotickingdoms.com\/blog\/wp-json\/wp\/v2\/categories?post=489"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.chaotickingdoms.com\/blog\/wp-json\/wp\/v2\/tags?post=489"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}