How to keep the VBScript command window open during execution - vbscript

When I execute a VBScript, the command window that it creates closes quickly before the user gets a chance to read the output. How can I get the window to stay open without modifying windows registry?
This is the code:
Set objShell = WScript.CreateObject("WScript.shell")
objShell.Run "SyncToyCmd.exe -R", 1, True

You can send your execution command through the cmd.exe command interpreter, along with a pause command which will give the user a Press any key to continue . . . prompt to close the window.
objShell.run "%comspec% /c ""SyncToyCmd.exe -R & pause""", 1, True
Or to keep the window alive, use the /k flag instead of /c:
objShell.run "%comspec% /k SyncToyCmd.exe -R", 1, True
But beware, your VBScript will not continue (or terminate) until this cmd window is manually closed.
The %comspec% environment variable refers to the correct command to open the command interpreter as per your operating system. On my XP machine, for instance, %comspec% is equal to C:\WINDOWS\system32\cmd.exe.
See cmd.exe documentation here: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/cmd.mspx?mfr=true
More info on the use of the & versus the && command separators here.

Assuming that it's the popped-up command window that you want to keep open (rather than the one running your VBScript), you can use CMD.exe's Pause command to achieve this:
Set objShell = WScript.CreateObject("WScript.shell")
objShell.Run "cmd.exe /C ""SyncToyCmd.exe -R & Pause"" ", 1, True

Make it sleep for a while, maybe tell the user it will close in 5 seconds?
Set WScript = CreateObject("WScript.Shell")
WScript.Sleep 5000

Related

Use "cmd /c" but hide the console window

I have a shortcut running this command when clicked: cmd /c "full path to my batch file". When I use it, it does what it is supposed to do, but in the process the ugly console window pops up. Is there any way to make this command start a hidden or at least minimized window?
Use the command start with switch /min to start cmd in minimized window:
start /min cmd /c "full path to my batch file"
powershell "start <path of batch file> -Args \"<batch file args>\" -WindowStyle Hidden"
This can be placed in a separate batch file which, when called, will terminate immediately while your batch file executes in the background.
From ' Args ' to ' \" ' can be excluded if your batch file has no arguments.
' -v runAs' can be added before the end quote to run your batch file as an administrator.
I found this solution :
Create a launch.vbs file and add
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\Batch Files\syncfiles.bat" & Chr(34), 0
Set WshShell = Nothing
Replace "C:\Batch Files\syncfiles.bat" by your absolute or relative path file name.
Source : https://www.winhelponline.com/blog/run-bat-files-invisibly-without-displaying-command-prompt/
Source MSDN : https://msdn.microsoft.com/en-us/library/d5fk67ky(VS.85).aspx
Right-click on the shortcut icon and choose "Properties."
On the "Shortcut" tab, choose the "Run" type you desire from the dropdown menu.
The START command has a /B switch to start without creating a window. Use START /? to read all about it.
This will create a separate process (without a window), and not block parent window so it can continue your main work:
start /b cmd /c "full path to my batch file"
Use AutoHotKey file. Download and install AutoHotKey first.
suppose you have an 1.bat
you'll create a C:\2.ahk, whose content is
Run C:\1.bat,,Hide
return
and you'll create a 3.lnk, and right click it, click property, then set the Target to
"C:\Program Files\AutoHotkey\AutoHotkey.exe" C:\2.ahk
Then you'll get what you want.
This way, you can attach the 3.lnk to your taskbar or start menu, and also change its icon.
The start method can only be used in a bat, which can't be added to taskbar or changed icon.
Create a VBScript file as a shell to start it.
' Launcher.vbs
If WScript.Arguments.Count = 0 Then
WScript.Quit 1
End If
Dim WSH
Set WSH = CreateObject("WScript.Shell")
WSH.Run "cmd /c " & WScript.Arguments(0), 0, False
You may want to embed this as a Here Document in your batch file. See heredoc for Windows batch?

Batch - start a cmd window with a command, but run in the background

The title says it all: i want to start a cmd window with a command, but i want the window hidden
start cmd.exe /k "my command"
This does what I want, but the cmd window remains open, and upon closing i end the command too. I want to run the cmd.exe in the background. Is it possible?
This question was answered here: How to run a command on the background on Windows?
Basically, you just need the /b option from the start command.
If that does not help, go the VB way, creating a .vbs like this:
Dim WinScriptHost
Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "C:\Scheduled Jobs\mybat.bat" & Chr(34), 0
Set WinScriptHost = Nothing

Simple shutdown script does not function properly after PC wipe

I had a simple VB Script that would let me enter in the amount of minutes before I wanted my PC to turn off by itself, and then it would auto-shutdown. That worked fine. After I wiped my PC, the script no longer functions as intended, instead showing a blank cmd window after I enter the number of minutes before shutdown, and displays the inputbox again (asking for # of minutes before shutdown).
Any ideas on why this won't function correctly, and why it worked before but not now? Do I need a certain package from Microsoft that maybe I didn't reinstall?
Code:
Dim a
Dim oShell
a=inputbox("After how many minutes would you like to shut down your PC? Enter cancel to cancel a previous shutdown")
Set oShell = WScript.CreateObject ("WScript.Shell")
if a = "cancel" then
oShell.run "cmd.exe /c shutdown /a"
elseif a = "" then
MsgBox"Please enter after how many minutes you would like to turn off this PC",0+16,"Enter a number"
elseif a = "0" then
b=msgbox("Are you sure you want to shut down this PC immediately?",4+32,"Shut down immediately?")
if b = "6" then
oShell.run "cmd.exe /c shutdown /s /f"
end if
else
oShell.run "cmd.exe /c shutdown /s /t " & (a * 60)
end if
EDIT: Running the script from its directory works as intended, but running the VBScript from a shortcut (as a I had been doing) doesn't work and yields the above results.
EDIT: Also the script itself won't run properly on my desktop, but runs fine in the folder I store my scripts.
You named the script shutdown.vbs and run it with the working directory set to the directory containing the script. By running oShell.Run "cmd.exe /c shutdown ..." your script is effectively calling itself.
If you call a command shutdown (without path and extension) the system is looking for a file with one of the extensions listed in %PATHEXT% in the directories listed in the %PATH% environment variable. The first match wins.
Since on Windows the current directory comes first in the %PATH% the file %CD%\shutdown.vbs is found before %windir%\system32\shutdown.exe.
Either rename your VBScript or change cmd.exe /c shutdown to cmd.exe /c shutdown.exe and the problem will disappear.

Command prompt opening multiple windows on wshShell.Run

Hi I'm trying to run following command in VBA
Set wshShell = CreateObject("WScript.Shell")
wshShell.Run "cmd.exe runas some commands"
It's opening multiple command prompt windows.
If I run
wshShell.Run "cmd.exe"
Only this then it opens a single window.
Am i doing anything wrong in the 1st scenario.
Try using the /K switch and keep the quotes aroung cmd.exe before you run the rest of your commands
wshShell.Run " 'cmd.exe /K' 'commands here'"

How to write to the same command prompt with vbs in windows?

I have vbs script and that creates folder, make archive and copy to that folder, upload to ftp and so on. I want it to write status to cmd after each step of execution( after creating folder, zip...)
The following opens cmd.exe and writes there "creates folder". That's exactly what I want.
Dim objShell, strCmd
strCmd = "%comspec% /k echo creates folder"
Set objShell = CreateObject("Wscript.Shell")
objShell.Run strCmd, 1, True
But, how I can write to the same cmd window that just opened? If I use this
strCmd = "%comspec% /k echo starting zip"
objShell.Run strCmd, 1, True
it opens new cmd window, but I want to write "starting zip" to previously opened cmd.
How I achieve this?
To print to the command prompt use wscript.echo.
I want to point out that the behavior of .echo is effected by how the script is loaded. For instance, if I run it from command prompt, like this: test.vbs, then the echo lines show up as pop-ups due to running wscript by default. However, if instead I load the file like this: cscript text.vbs all output goes to console as expected.

Resources