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.
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 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?
In batch file I have the command below:
WScript ABC.vbs
In ABC.vbs:
Set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Echo "Hello"
When I run the batch file there is a pop-up message with the text "Hello". But what I want is showing the message "Hello" in the Command Prompt window like I do the right click on ABC.vbs and then select "Open with command prompt".
In addition to the two comments to use cscript.exe ABC.vbs as your command line, here is a function you can put in your .vbs script to ensure it always runs with the cscript engine, no matter how it's called.
Sub checkengine
pcengine = LCase(Mid(WScript.FullName, InstrRev(WScript.FullName,"\")+1))
' BEGIN CALLOUT A
If Not pcengine="cscript.exe" Then
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "CSCRIPT.EXE """ & WScript.ScriptFullName & """"
WScript.Quit
End If
' END CALLOUT A
End Sub
From this site: Forcing VBScript Files to Run in CScript Mode
Put Call checkengine at the beginning of your vbscript. If it detects that cscript.exe is not in the command line, it relaunches the script with that engine.
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'"
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