(view source code of vbs2cmd.vbs as plain text)
'Author: Denis St-Pierre (Ottawa, Canada)
'Purpose: Ever wanted to run a VBS script from a CMD but do it all from ONE file.
' This converts a VBS file into a CMD Subroutine that recreates
' The VBS code in \%TEMP%\ folder and launches it from inside that CMD.
' The name of the Subroutine matches the name of the VBS file.
'
' DISCLAIMER: Use at your own risk!
' The author and whatever website you got this from is NOT responsible for anything that could go wrong
'
' License: Public Domain
'History:
' 1.0 2Feb2010 Initial release
' 1.1 30May2010 Command line arguments added by Rob van der Woude, to allow use in non-XP Windows versions
' 1.2 7Aug2014 Bug with output file names in non-root folders fixed by Rob van der Woude
'
'Features: Handles symbols that needs to be escaped for CMD's Echo to work
' Output is a fully functional CMD script
Option Explicit
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const TristateUseDefault = -2
Dim arrVBStext
Dim blnQuiet
Dim intOVRWT, intResult, intValidArgs
Dim objDialog, objFile, objFSO, wshShell
Dim strBaseFileName, strCMDout, strFileName, strFileNameIN, strFileNameOUT
Dim strNewText, strOldText, strText, strVBSline
With WScript.Arguments
If .Unnamed.Count > 1 Then Syntax
intValidArgs = 0
If .Unnamed.Count = 1 Then
strFileNameIN = .Unnamed(0)
intValidArgs = intValidArgs + 1
End If
If .Named.Exists( "Q" ) Then
blnQuiet = True
intValidArgs = intValidArgs + 1
End If
If intValidArgs <> .Count Then Syntax
End With
Set WshShell = WScript.CreateObject( "WScript.Shell" )
Set objFSO = CreateObject( "Scripting.FileSystemObject" )
If strFileNameIN = "" Then
'File browse dialog box (XP only) (NFG in 7/2008, Thank you MS!)
On Error Resume Next
Set objDialog = CreateObject( "UserAccounts.CommonDialog" )
If Err Then
strFileNameIN = ""
WScript.Echo "No file specified."
Else
objDialog.Filter = "All Files|*.*"
objDialog.InitialDir = wshShell.CurrentDirectory
intResult = objDialog.ShowOpen
If intResult = 0 Then
strFileNameIN = ""
wshShell.Popup "No file selected.", 1, " ", 64
Else
strFileNameIN = objDialog.FileName
End If
Set objDialog = Nothing
End If
Set objDialog = Nothing
On Error Goto 0
End If
If strFileNameIN = "" Then Syntax
strFileNameOUT = objFSO.Buildpath( objFSO.GetParentFolderName( strFileNameIN ), objFSO.GetBaseName( strFileNameIN ) & "_CONVERTED.CMD" )
'Check if strFileNameOUT exists already
If objFSO.FileExists( strFileNameOUT ) Then 'does the file EXIST?
If blnQuiet Then
WScript.Echo "Deleting existing file."
objFSO.DeleteFile( strFileNameOUT )
Else
intOVRWT = MsgBox( strFileNameOUT & " exists already" & vbCrLf & "Overwrite?", vbYesNoCancel, "Overwrite?" )
If intOVRWT = 6 Then
'proceed
objFSO.DeleteFile( strFileNameOUT )
Else
wshShell.Popup "Exiting as requested.", 1, " ", 64
WScript.Quit
End If
End If
End If
'open strFileNameANSI file, and put entire file into a variable
Set objFile = objFSO.OpenTextFile( strFileNameIN, ForReading )
strText = objFile.ReadAll
objFile.Close
'************ Start converting *************
'^ Escape character.
' Adding the escape character before a command symbol
' allows it to be treated as ordinary text.
'When piping or redirecting any of these characters you should
'prefix with the escape ^ character: \ & | > < ^
'e.g. ^\ ^& ^| ^> ^< ^^
'# Escape out ^ symbols (Must be 1st !!!)
strOldText = "^"
strNewText = "^^"
strText = Replace( strText, strOldText, strNewText )
'# Escape out \ symbols
'strOldText = "\"
'strNewText = "^\"
'strText = Replace( strText, strOldText, strNewText )
'# Escape out & symbols
strOldText = "&"
strNewText = "^&"
strText = Replace( strText, strOldText, strNewText )
'# Escape out | symbols
strOldText = "|"
strNewText = "^|"
strText = Replace( strText, strOldText, strNewText )
'# Escape out > symbols
strOldText = ">"
strNewText = "^>"
strText = Replace(strText, strOldText, strNewText)
'# Escape out < symbols
strOldText = "<"
strNewText = "^<"
strText = Replace( strText, strOldText, strNewText )
'Converting into array
'Dim arrVBStext()
arrVBStext = Split( strText, vbCrLf ) 'create one-dimensional array
strFileName = objFSO.GetFileName( strFileNameIN )
strBaseFileName = objFSO.GetBaseName( strFileNameIN )
strCMDout = ""
strCMDout = strCMDout & "@ECHO OFF" & vbCrLf
strCMDout = strCMDout & "Call :" & strBaseFileName & vbCrLf
strCMDout = strCMDout & vbCrLf
strCMDout = strCMDout & vbCrLf
strCMDout = strCMDout & "REM Prevent running the " & strFileName & " twice" & vbCrLf
strCMDout = strCMDout & "Exit /b 0" & vbCrLf
strCMDout = strCMDout & ":" & strBaseFileName & vbCrLf
strCMDout = strCMDout & "REM This will create a file called " & strFileName & " in %TEMP%" & vbCrLf
strCMDout = strCMDout & "REM" & vbCrLf
strCMDout = strCMDout & "REM The following will overwite any pre-existing file called %TEMP%\" & strFileName & vbCrLf
strCMDout = strCMDout & "echo.> %TEMP%\" & strFileName & vbCrLf
'Add Echo in front and >> %TEMP%\<VBSNAME>.VBS at the end of every line
For Each strVBSline in arrVBStext
If Trim( strVBSline ) = "" Then
strCMDout = strCMDout & "echo. >> %TEMP%\" & strFileName & vbCrLf
Else
strCMDout = strCMDout & "echo " & strVBSline & " >> %TEMP%\" & strFileName & vbCrLf
End If
Next
strCMDout = strCMDout & "Cscript.exe //NoLogo ""%TEMP%\" & strFileName & """" & vbCrLf
strCMDout = strCMDout & "Exit /b 0" & vbCrLf
'Converting done
'Write to file
Set objFile = objFSO.OpenTextFile( strFileNameOUT, ForAppending, True )
objFile.WriteLine strCMDout
objFile.Close
If blnQuiet Then
WScript.Echo "created " & strFileNameOUT
Else
wshShell.Popup "created " & strFileNameOUT, 3, "Completed", 64
End If
Set objFile = Nothing
Set wshShell = Nothing
Set objFSO = Nothing
Sub Syntax
Dim strMsg
strMsg = vbCrLf _
& "Usage: VBS2CMD.VBS vbscript_file.vbs [ /Q ]" & vbCrLf & vbCrLf _
& "Where: vbscript_file.vbs is the file to be ""converted""" & vbCrLf _
& " (mandatory in all Windows versions except XP)" & vbCrLf _
& " /Q prevents popup dialogs"
WScript.Echo strMsg
WScript.Quit 1
End Sub
page last modified: 2024-04-16; loaded in 0.0148 seconds