{"id":7,"date":"2008-02-14T12:53:16","date_gmt":"2008-02-14T17:53:16","guid":{"rendered":"http:\/\/blog.bitsofgenius.com\/?p=7"},"modified":"2011-01-26T15:11:59","modified_gmt":"2011-01-26T19:11:59","slug":"vbscript-code-to-beautify-vbscript-code","status":"publish","type":"post","link":"https:\/\/blog.bitsofgenius.com\/?p=7","title":{"rendered":"VBScript code to beautify VBScript code"},"content":{"rendered":"<p>Sounds redundant, but it&#8217;s a utility function you can use when NotePad, TextPad, Context or another text editor (e.g. textarea of a browser) is used for developing the script.  I&#8217;ve used this function in a web page which allows building and testing small utility functions.  In that page, once the code is submitted and executed, it returns the results and the beautified code.  The script writer can stay focused on functionality&#8211;not format.<\/p>\n<pre lang=\"vb\" line=\"1\" escaped=\"true\">\r\nFunction BeautifyVBS (sSource, nTabSpacing)\r\n' Takes VBScript source code and rebuilds the indentation.\r\nDim sRawLine, sLine, sTest, iIndentIndex, iIndex, oS, sWhiteSpace, bAdjustIndent, bInQuote, aRows\r\n\r\nDim aKey(34)\r\n\r\nConst INDENT_String = 0\r\nConst INDENT_Exeception_String = 1\r\nConst INDENT_Pre_Indent = 2\r\nConst INDENT_Post_Indent = 3\r\n\r\n' The indent and unindent list is as complete as I could make it (from the MS VBScript reference).\r\naKey (0) = ARRAY (\"if \", \" then\", 0, 1)\r\naKey (1) = ARRAY (\"select \", \"\", 0, 2)\r\naKey (2) = ARRAY (\"sub \", \"\", 0, 1)\r\naKey (3) = ARRAY (\"function \", \"\", 0, 1)\r\naKey (4) = ARRAY (\"do \",\"\", 0, 1)\r\naKey (5) = ARRAY (\"while \",\"\", 0, 1)\r\naKey (6) = ARRAY (\"for \",\"\", 0, 1)\r\naKey (7) = ARRAY (\"case \", \"\", -1, 1)\r\naKey (8) = ARRAY (\"with \",\"\", 0, 1)\r\naKey (9) = ARRAY (\"class \",\"\", 0, 1)\r\naKey (10) = ARRAY (\"public sub\",\"\", 0, 1)\r\naKey (11) = ARRAY (\"private sub \",\"\", 0, 1)\r\naKey (12) = ARRAY (\"public function \",\"\", 0, 1)\r\naKey (13) = ARRAY (\"private function \",\"\", 0, 1)\r\naKey (14) = ARRAY (\"property get \",\"\", 0, 1)\r\naKey (15) = ARRAY (\"public property get \",\"\", 0, 1)\r\naKey (16) = ARRAY (\"private property get \",\"\", 0, 1)\r\naKey (17) = ARRAY (\"property let \",\"\", 0, 1)\r\naKey (18) = ARRAY (\"public property let \",\"\", 0, 1)\r\naKey (19) = ARRAY (\"private property let \",\"\", 0, 1)\r\naKey (20) = ARRAY (\"property set \",\"\", 0, 1)\r\naKey (21) = ARRAY (\"public property set \",\"\", 0, 1)\r\naKey (22) = ARRAY (\"private property set \",\"\", 0, 1)\r\naKey (23) = ARRAY (\"else \",\"\", -1, 1)\r\naKey (24) = ARRAY (\"elseif \",\"\", -1, 1)\r\naKey (25) = ARRAY (\"end if\", \"\", -1, 0)\r\naKey (26) = ARRAY (\"end select\", \"\", -2, 0)\r\naKey (27) = ARRAY (\"end sub\", \"\", -1, 0)\r\naKey (28) = ARRAY (\"end function\", \"\", -1, 0)\r\naKey (29) = ARRAY (\"loop\", \"\", -1, 0)\r\naKey (30) = ARRAY (\"wend\", \"\", -1, 0)\r\naKey (31) = ARRAY (\"next\", \"\", -1, 0)\r\naKey (32) = ARRAY (\"end class\", \"\", -1, 0)\r\naKey (33) = ARRAY (\"end property\", \"\", -1, 0)\r\naKey (34) = ARRAY (\"end with\", \"\", -1, 0)\r\n\r\nsWhiteSpace = \" \" &amp; vbTab\r\n\r\nSet oS = CreateObject(\"ADODB.Stream\")\r\noS.Type = 2   ' ASCII\r\noS.Open\r\n\r\niIndentIndex = 0\r\nFor Each sRawLine in Split (sSource, vbCrLf)\r\n\r\n' Remove all whitespace on the left\r\niIndex = 1\r\nIf Len (sRawLine) &gt; 0 Then\r\nDo While iIndex &lt;= Len (sRawLine)\r\nIf Instr (sWhiteSpace, Mid (sRawLine, iIndex, 1)) = 0 Then\r\nExit Do\r\nEnd If\r\niIndex = iIndex + 1\r\nLoop\r\nEnd If\r\nIf iIndex &gt; Len (sRawLine) Then\r\nsLine = \"\"\r\nElse\r\nsLine = Mid (sRawLine, iIndex)\r\nEnd If\r\n\r\n' Remove all whitespace on the right\r\niIndex = Len (sLine)\r\nDo While iIndex &gt; 0\r\nIf Instr (sWhiteSpace, Mid (sLine, iIndex, 1)) = 0 Then\r\nExit Do\r\nEnd If\r\niIndex = iIndex - 1\r\nLoop\r\nIf iIndex &lt; Len (sLine) Then\r\nsLine = Left (sLine, iIndex)\r\nEnd If\r\n\r\nsTest = LCase (LTrim (sLine))\r\n' Find any in-line comment marker, and truncate the comment if it exists.\r\nbInQuote = False\r\nFor iIndex = 1 To Len (sTest)\r\nIf Not bInQuote And Mid (sTest, iIndex, 1) = \"'\" Then\r\nExit For\r\nEnd If\r\nIf Mid (sTest, iIndex, 1) = \"\"\"\" Then\r\nbInQuote = Not bInQuote\r\nEnd If\r\nNext\r\nIf iIndex &lt; Len (sTest) Then  ' Truncate comment\r\nsTest = Left (sTest, iIndex - 1)\r\n' Truncate whitespace again\r\niIndex = Len (sTest)\r\nDo While iIndex &gt; 0\r\nIf Instr (sWhiteSpace, Mid (sTest, iIndex, 1)) = 0 Then\r\nExit Do\r\nEnd If\r\niIndex = iIndex - 1\r\nLoop\r\nIf iIndex &lt; Len (sTest) Then\r\nsTest = Left (sTest, iIndex)\r\nEnd If\r\nEnd If\r\n\r\nsTest = LCase (LTrim (sTest)) &amp; SPACE (32)\r\n\r\n' Adjust Indentation as needed\r\nbAdjustIndent = False\r\nFor iIndex = 0 To UBound (aKey, 1)\r\nIf Left (sTest, LEN (aKey(iIndex)(INDENT_String))) = aKey(iIndex)(INDENT_String) Then\r\nIf LEN(aKey(iIndex)(INDENT_Exeception_String)) = 0 Or Right (RTrim (sTest), LEN (aKey(iIndex)(INDENT_Exeception_String))) = aKey(iIndex)(INDENT_Exeception_String) Then\r\nbAdjustIndent = True\r\nExit For\r\nEnd If\r\nEnd If\r\nNext\r\n\r\nIf bAdjustIndent Then\r\niIndentIndex = iIndentIndex + aKey(iIndex)(INDENT_Pre_Indent)\r\nIf iIndentIndex &lt; 0 Then iIndentIndex = 0\r\nEnd If\r\n\r\nIf nTabSpacing &lt;= 0 Then\r\noS.WriteText STRING (iIndentIndex, vbTab) &amp; sLine &amp; vbCrLf\r\nElse\r\noS.WriteText SPACE (nTabSpacing * iIndentIndex) &amp; sLine &amp; vbCrLf\r\nEnd If\r\n\r\nIf bAdjustIndent Then\r\niIndentIndex = iIndentIndex + aKey(iIndex)(INDENT_Post_Indent)\r\nIf iIndentIndex &lt; 0 Then iIndentIndex = 0\r\nEnd If\r\nNext\r\n\r\noS.Position = 0\r\nBeautifyVBS = oS.ReadText (-1)\r\noS.Close()\r\nSet oS = Nothing\r\nEnd Function\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Sounds redundant, but it&#8217;s a utility function you can use when NotePad, TextPad, Context or another text editor (e.g. textarea of a browser) is used for developing the script. I&#8217;ve used this function in a web page which allows building and testing small utility functions. In that page, once the code is submitted and executed, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-7","post","type-post","status-publish","format-standard","hentry","category-vbscript-functions-subs"],"_links":{"self":[{"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=\/wp\/v2\/posts\/7","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7"}],"version-history":[{"count":1,"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=\/wp\/v2\/posts\/7\/revisions"}],"predecessor-version":[{"id":430,"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=\/wp\/v2\/posts\/7\/revisions\/430"}],"wp:attachment":[{"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}