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'"
Related
So I've been working on a project, and I want the program to run a file that executes a command when I start-up my computer.
Set oShell = WScript.CreateObject ("WScript.Shell")
oShell.run "cmd.exe" ""
Set oShell = Nothing
I do not know what to write in "" to make the start-up VBS file launch the Command Line and execute a command on start-up. Can someone help?
The following VBScript code opens a command window, changes to the path to C:\ , and executes the DIR command.
oShell.run "cmd /K CD C:\ & Dir"
CMD /C Run Command and then terminate
CMD /K Run Command and then return to the CMD prompt.
This is useful for testing, to examine variables
More information:
CMD.exe
I want to write a simple vb script to automate shutdown in windows.
the code I am using is :
Dim ti
ti=InputBox("enter time in minutes")
ti=ti*60
Set objShell=CreateObject("WScript.Shell")
objShell.Run "cmd shutdown /s /t "& ti & " "
but when I enter the time and press enter , all I get is an command prompt window and nothing happens
I even tried by setting a default value for time and specifing the complete path for shutdown.exe ,but nothing seems to be working
Set WshShell = WScript.CreateObject("WScript.Shell")
Command = "C:\Windows\System32 shutdown.exe -s -t 600 "
WshShell.Run Command
can u please correct me and guide me towards the right code ....
It looks like you're missing a backslash in your path:
Set WshShell = WScript.CreateObject("WScript.Shell")
Command = "C:\Windows\System32\shutdown.exe -s -t 600 "
WshShell.Run Command
If you want to run commands in cmd you have to use either /k (keep cmd window open after command finishes) or /c (close cmd window after command finishes). Here's the canonical way to do this:
ti = InputBox("enter time in minutes")
ti = ti * 60
CreateObject("WScript.Shell").Run "%COMSPEC% /c shutdown -s -t " & ti
%COMSPEC% is a system environment variable with the path to cmd.exe.
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.
How do I ran a script after I open a command window using the below script?
Set objSh = CreateObject("Shell.Application")
objSh.ShellExecute "cmd.exe", "uac" , "", "runas", 1
For example, how do I run ipconfig as an admin using the above script?
If you specify the /c switch, then cmd.exe will carry out the specified command and then terminate.
So, for example:
Set objSh = CreateObject("Shell.Application")
objSh.ShellExecute "cmd.exe /c ipconfig", "uac" , "", "runas", 1
Alternatively, you could use the /k switch, which works exactly the same way, except it keeps the command prompt on the screen once your command finishes executing.
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