Run Hidden Script (for Bat files)

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

Run Hidden Script (for Bat files)

Post by MigrationUser »

11 Aug 2012 10:21
xnu


Hello,

I need two vbs scripts for hiding cmd prompts.

One of them would work with arguments.
I mean, like this:

Code: Select all

hide.vbs backup.bat
(it shall execute "backup.bat" without any windows)

---

And another one would just execute a bat file without a window:

Code: Select all

hide_backup.vbs
(it shall call a cmd file in a hidden window)

I made searches on Google. I find a few scripts, but i don't know which are the best.
Since I'm not a professional I need your help.
I would really appreciate if you tell me which scripts are the simplest and the most reliable.

Here are them

With Arguments:

Code: Select all

CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False

Code: Select all

dim arg
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objArgs = WScript.Arguments
arg=objArgs(0)
Set objArgs = WScript.Arguments
For I = 1 to objArgs.Count - 1
   arg = arg & " " & objArgs(I)
Next
Return = WshShell.Run(arg, 0, False)
set WshShell = Nothing

Code: Select all

Option Explicit

On Error Resume Next

Dim oSh, sCmd, i

If Not IsWscript() Then
  WScript.Echo "Please run this script using WSCRIPT."
  WScript.Quit(1)
End If

If ParseCmdLine(sCmd) Then
  Set oSh = CreateObject("WScript.Shell")
  oSh.Run Trim(sCmd), 0, False
End If

WScript.Quit(0)


Private Function ParseCmdLine(sCmd)

  Dim i

  ParseCmdLine = False

  If WScript.Arguments.Count > 0 Then
    Select Case WScript.Arguments(0)
      Case "/?" : ShowUsage(True)
      Case "-?" : ShowUsage(True)
      Case "/h" : ShowUsage(True)
      Case "-h" : ShowUsage(True)
      Case "h"  : ShowUsage(True)
      Case Else : For i = 0 to WScript.Arguments.Count -1
                    If InStr(WScript.Arguments(i), " ") Then
                      sCmd = sCmd & " " & Chr(34) & WScript.Arguments(i) & Chr(34)
                    Else
                      sCmd = sCmd & " " & WScript.Arguments(i)
                    End If
                  Next
                  ParseCmdLine = True
    End Select
  Else
    ShowUsage(True)
  End If

  ParseCmdLine = True

End Function


Private Function IsWscript()

  IsWscript = False

  If InStrRev(LCase(WScript.FullName), "wscript.exe", -1) Then
    IsWscript = True
  End If

End Function


Private Sub ShowUsage(bExit)

  Dim strHelp

  strHelp = "Creates a new process that executes in a hidden window." & _
            vbCrLf & vbCrLf & _
            "RunHide Command [Arg1 [" & Chr(34) & "Arg 2" & Chr(34) & " [..."
  MsgBox strHelp, 64, "RunHide Syntax"

  If bExit Then
    WScript.Quit(1)
  End If

End Sub
Without arguments:

Code: Select all

CreateObject("Wscript.Shell").Run "backup.bat", 0, False

Dim objWsh
Set objWsh = CreateObject( "WScript.Shell" )
objWsh.Run "backup.bat" , 0 , 0
Set(objWsh)=Nothing

Set oShell = CreateObject ("Wscript.Shell") 
Dim strArgs
strArgs = "cmd /c backup.bat"
oShell.Run strArgs, 0, false
Are the first (and the shortest) ones are reliable?
And if you can improve them, please do.

Thanks in advance.
And sorry for my bad English.

----------------------------

#2 11 Sep 2012 15:32
Misterd


If this is for Windows XP, you are in luck.

Code: Select all

fnShellExecuteVB

    function fnShellExecuteVB()
        dim objShell

        set objShell = CreateObject("Shell.Application") 'XP only!

        objShell.ShellExecute "notepad.exe", "", "", "open", 1

        set objShell = nothing
    end function
    
'How to use
'<object>.ShellExecute(sFile [, vArguments] [, vDirectory] [, vOperation] [, vShow])
'sFile	Required. A String that contains the name of the file on which ShellExecute will perform the action specified by vOperation.
'vArguments	Optional. A Variant that contains the parameter values for the operation.
'vDirectory	Optional. A Variant that contains the fully qualified path of the directory that contains the file specified by sFile. If this parameter is not specified, the current working directory is used.
'vOperation	Optional. A Variant that specifies the operation to be performed. It should be set to one of the verb strings that is supported by the file. For a discussion of verbs, see the Remarks section. If this parameter is not specified, the default operation is performed.
'vShow	Optional. A Variant that recommends how the window that belongs to the application that performs the operation should be displayed initially. The application can ignore this recommendation. vShow can take one of the following values. If this parameter is not specified, the application uses its default value.
'0	Open the application with a hidden window.
'1	Open the application with a normal window. If the window is minimized or maximized, the system restores it to its original size and position.
'2	Open the application with a minimized window.
'3	Open the application with a maximized window.
'4	Open the application with its window at its most recent size and position. The active window remains active.
'5	Open the application with its window at its current size and position.
'7	Open the application with a minimized window. The active window remains active.
'10	Open the application with its window in the default state specified by the application.

If this is for Windows 7, you are SOL with native stuff. :wall:
This is because MS didn't want to buy license to a DLL and wanted to push people to PowersHell
(comdlg32.ocx is a licensed control)

You might be able to borrow the DLL from XP to \sysWOW64 folder in windows 7 and register it using "REGSVR32 comdlg32.ocx" in Windows 7 for this code to work.

:twisted: :twisted: :twisted: The Catch: comdlg32.ocx is 32-bit ONLY. :twisted: :twisted: :twisted:
So your script would have to run with \sysWOW64\wscript.exe or cscript.exe

original thread: https://ss64.org/oldforum/viewtopic.php?id=1510
Post Reply