Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for vbs2cmd.vbs

(view source code of vbs2cmd.vbs as plain text)

  1. 'Author:  Denis St-Pierre (Ottawa, Canada)
  2. 'Purpose: Ever wanted to run a VBS script from a CMD but do it all from ONE file.
  3. '         This converts a VBS file into a CMD Subroutine that recreates
  4. '         The VBS code in \%TEMP%\ folder and launches it from inside that CMD.
  5. '         The name of the Subroutine matches the name of the VBS file.
  6. '
  7. '         DISCLAIMER:    Use at your own risk! 
  8. '         The author and whatever website you got this from is NOT responsible for anything that could go wrong
  9. '
  10. '         License:    Public Domain
  11.  
  12. 'History:
  13. '    1.0    2Feb2010    Initial release
  14. '    1.1    30May2010   Command line arguments added by Rob van der Woude, to allow use in non-XP Windows versions
  15. '    1.2    7Aug2014    Bug with output file names in non-root folders fixed by Rob van der Woude
  16. '
  17. 'Features:    Handles symbols that needs to be escaped for CMD's Echo to work
  18. '             Output is a fully functional CMD script
  19.  
  20. Option Explicit
  21.  
  22. Const ForReading = 1
  23. Const ForWriting = 2
  24. Const ForAppending = 8
  25. Const TristateUseDefault = -2
  26.  
  27. Dim arrVBStext
  28. Dim blnQuiet
  29. Dim intOVRWT, intResult, intValidArgs
  30. Dim objDialog, objFile, objFSO, wshShell
  31. Dim strBaseFileName, strCMDout, strFileName, strFileNameIN, strFileNameOUT
  32. Dim strNewText, strOldText, strText, strVBSline
  33.  
  34. With WScript.Arguments
  35. 	If .Unnamed.Count > 1 Then Syntax
  36. 	intValidArgs = 0
  37. 	If .Unnamed.Count = 1 Then
  38. 		strFileNameIN = .Unnamed(0)
  39. 		intValidArgs = intValidArgs + 1
  40. 	End If
  41. 	If .Named.Exists( "Q" ) Then
  42. 		blnQuiet = True
  43. 		intValidArgs = intValidArgs + 1
  44. 	End If
  45. 	If intValidArgs <> .Count Then Syntax
  46. End With
  47.  
  48. Set WshShell = WScript.CreateObject( "WScript.Shell" )
  49. Set objFSO = CreateObject( "Scripting.FileSystemObject" )
  50.  
  51. If strFileNameIN = "" Then
  52. 	'File browse dialog box (XP only) (NFG in 7/2008, Thank you MS!)
  53. 	On Error Resume Next
  54. 	Set objDialog = CreateObject( "UserAccounts.CommonDialog" )
  55. 	If Err Then
  56. 		strFileNameIN = ""
  57. 		WScript.Echo "No file specified."
  58. 	Else
  59. 		objDialog.Filter = "All Files|*.*"
  60. 		objDialog.InitialDir = wshShell.CurrentDirectory
  61. 		intResult = objDialog.ShowOpen
  62. 		If intResult = 0 Then
  63. 			strFileNameIN = ""
  64. 			wshShell.Popup "No file selected.", 1, " ", 64
  65. 		Else
  66. 			strFileNameIN = objDialog.FileName
  67. 		End If
  68. 		Set objDialog = Nothing
  69. 	End If
  70.  
  71. 	Set objDialog = Nothing
  72. 	On Error Goto 0
  73. End If
  74.  
  75. If strFileNameIN = "" Then Syntax
  76.  
  77. strFileNameOUT = objFSO.Buildpath( objFSO.GetParentFolderName( strFileNameIN ), objFSO.GetBaseName( strFileNameIN ) & "_CONVERTED.CMD" )
  78.  
  79. 'Check if strFileNameOUT exists already
  80. If objFSO.FileExists( strFileNameOUT ) Then  'does the file EXIST?
  81. 	If blnQuiet Then
  82. 		WScript.Echo "Deleting existing file."
  83. 		objFSO.DeleteFile( strFileNameOUT )
  84. 	Else
  85. 		intOVRWT = MsgBox( strFileNameOUT & " exists already" & vbCrLf & "Overwrite?", vbYesNoCancel, "Overwrite?" )
  86. 		If intOVRWT = 6 Then
  87. 			'proceed
  88. 			objFSO.DeleteFile( strFileNameOUT )
  89. 		Else
  90. 			wshShell.Popup "Exiting as requested.", 1, " ", 64
  91. 			WScript.Quit
  92. 		End If
  93. 	End If 
  94. End If
  95.  
  96.  
  97. 'open strFileNameANSI file, and put entire file into a variable
  98. Set objFile = objFSO.OpenTextFile( strFileNameIN, ForReading )
  99. strText = objFile.ReadAll
  100. objFile.Close
  101.  
  102. '************ Start converting *************
  103. '^  Escape character.
  104. ' Adding the escape character before a command symbol
  105. ' allows it to be treated as ordinary text. 
  106. 'When piping or redirecting any of these characters you should
  107. 'prefix with the escape ^ character: \ & | > < ^
  108. 'e.g.  ^\  ^&  ^|  ^>  ^<  ^^ 
  109.  
  110. '# Escape out ^ symbols (Must be 1st !!!)
  111. strOldText = "^"
  112. strNewText = "^^"
  113. strText = Replace( strText, strOldText, strNewText )
  114.  
  115. '# Escape out \ symbols
  116. 'strOldText = "\"
  117. 'strNewText = "^\"
  118. 'strText = Replace( strText, strOldText, strNewText )
  119.  
  120. '# Escape out & symbols
  121. strOldText = "&"
  122. strNewText = "^&"
  123. strText = Replace( strText, strOldText, strNewText )
  124.  
  125. '# Escape out | symbols
  126. strOldText = "|"
  127. strNewText = "^|"
  128. strText = Replace( strText, strOldText, strNewText )
  129.  
  130. '# Escape out > symbols
  131. strOldText = ">"
  132. strNewText = "^>"
  133. strText = Replace(strText, strOldText, strNewText)
  134.  
  135. '# Escape out < symbols
  136. strOldText = "<"
  137. strNewText = "^<"
  138. strText = Replace( strText, strOldText, strNewText )
  139.  
  140. 'Converting into array
  141. 'Dim arrVBStext()
  142. arrVBStext = Split( strText, vbCrLf )	'create one-dimensional array
  143.  
  144. strFileName = objFSO.GetFileName( strFileNameIN )
  145. strBaseFileName = objFSO.GetBaseName( strFileNameIN )
  146. strCMDout = ""
  147. strCMDout = strCMDout & "@ECHO OFF" & vbCrLf
  148. strCMDout = strCMDout & "Call :" & strBaseFileName & vbCrLf
  149. strCMDout = strCMDout & vbCrLf
  150. strCMDout = strCMDout & vbCrLf
  151. strCMDout = strCMDout & "REM Prevent running the " & strFileName & " twice" & vbCrLf
  152. strCMDout = strCMDout & "Exit /b 0" & vbCrLf
  153.  
  154. strCMDout = strCMDout & ":" & strBaseFileName & vbCrLf
  155.  
  156. strCMDout = strCMDout & "REM  This will create a file called " & strFileName & " in %TEMP%" & vbCrLf
  157. strCMDout = strCMDout & "REM" & vbCrLf
  158. strCMDout = strCMDout & "REM  The following will overwite any pre-existing file called %TEMP%\" & strFileName & vbCrLf
  159. strCMDout = strCMDout & "echo.> %TEMP%\" & strFileName & vbCrLf
  160. 'Add  Echo  in front and  >> %TEMP%\<VBSNAME>.VBS at the end of every line
  161. For Each strVBSline in arrVBStext
  162. 	If Trim( strVBSline ) = "" Then
  163. 		strCMDout = strCMDout & "echo. >> %TEMP%\" & strFileName & vbCrLf
  164. 	Else
  165. 		strCMDout = strCMDout & "echo " & strVBSline & " >> %TEMP%\" & strFileName & vbCrLf
  166. 	End If
  167. Next
  168. strCMDout = strCMDout & "Cscript.exe //NoLogo ""%TEMP%\" & strFileName & """" & vbCrLf
  169. strCMDout = strCMDout & "Exit /b 0" & vbCrLf
  170. 'Converting done
  171.  
  172. 'Write to file
  173. Set objFile = objFSO.OpenTextFile( strFileNameOUT, ForAppending, True )
  174. objFile.WriteLine strCMDout
  175.  
  176. objFile.Close
  177.  
  178. If blnQuiet Then
  179. 	WScript.Echo "created " & strFileNameOUT
  180. Else
  181. 	wshShell.Popup "created " & strFileNameOUT, 3, "Completed", 64
  182. End If
  183.  
  184. Set objFile = Nothing
  185. Set wshShell = Nothing
  186. Set objFSO = Nothing
  187.  
  188.  
  189. Sub Syntax
  190. 	Dim strMsg
  191. 	strMsg = vbCrLf _
  192. 	       & "Usage:  VBS2CMD.VBS  vbscript_file.vbs  [ /Q ]" & vbCrLf & vbCrLf _
  193. 	       & "Where:  vbscript_file.vbs  is the file to be ""converted""" & vbCrLf _
  194. 	       & "                           (mandatory in all Windows versions except XP)" & vbCrLf _
  195. 	       & "        /Q                 prevents popup dialogs"
  196. 	WScript.Echo strMsg
  197. 	WScript.Quit 1
  198. End Sub
  199.  

page last modified: 2024-04-16; loaded in 0.0148 seconds