Multiple commands in command prompt using vbscript - vbscript

Set oShell = CreateObject("WScript.Shell")
oShell.Run "cmd /c c:"
This line executes perfectly fine. Now I need to enter a text.
For example: c:\users> "abcd"
How do I go about it in the already opened cmd prompt.

You must add & after each command and change cmd /c to cmd /k
The first command is : CD /D c:\
The second command is : Dir
The third command is : ping 127.0.0.1
Try like this :
Set oShell = CreateObject("WScript.Shell")
Command = "cmd /K cd /d c:\ & Dir & ping 127.0.0.1"
oShell.Run Command,1,True

Related

How to make a start-up file launch a Command Line command on Start-Up?

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

How change PATH env in cmd.exe /c in Windows

I want run something like this:
cmd /c "set ""Path=V:\;%Path%"" & ECHO. %Path% & PAUSE"
But there is a problem: the command line window appears only for a moment, then disappears. I create a test .bat (like echo.1: %1 and 2,3...) to check out how it behave and...
0: "V:\test.bat"
1: /c
2: "set ""Path=V:\;
3:
(4-9 the same)
When I replace % with for example 5, second argument look like "set ""Path=V:\;5Path5"" & ECHO. 5Path5 & PAUSE", but it is not what I want.
I think, there may be problem with parsing %Path% inside this shell command.
I tried aslo:
cmd /c "set ""Path=V:\;%%Path%%"" & ECHO. %%Path%% & PAUSE"
cmd /c "set ""Path=V:\;%%%Path%%%"" & ECHO. %%%Path%%% & PAUSE"
cmd /c "set ""Path=V:\;%%%%Path%%%%"" & ECHO. %%%%Path%%%% & PAUSE"
cmd /c "set ""Path=V:\;%%%%%Path%%%%%"" & ECHO. %%Path%%%%% & PAUSE"
cmd /c "set ""Path=V:\;%%%%%%%%Path%%%%%%%%"" & ECHO. %%%%%%%%Path%%%%%%%% & PAUSE"
cmd /c "set ""Path=V:\;^%Path^%"" &ECHO.^%Path^%&PAUSE"
cmd /c "set ""Path=V:\;^%%Path^%%"" &ECHO.^%Path^%&PAUSE"
cmd /c "set ""Path=V:\;\%Path\%"" &ECHO.\%Path\%&PAUSE"
I will editing post during my next tests...
Edit:
I just noticed that I cannot even use simply: cmd /c "echo %PATH% & PAUSE"
I come to the solution. What I needed:
cmd /c "set ""Path=V:\;%Path:~0%"" &ECHO.%Path:~0%&PAUSE"
It's look like Windows replace %PATH% with this env-variable content (more paths by semicolons), even in Run (Win+R), CMD (.exe), ShellExec (wscript/winapi). So it can not be used as part of argument in shortcut (i needed to).
That doesn't seem to work because xx is evaluated before cmd is started
cmd /c "set xx=yyyyyy & echo %xx% & PAUSE"
That proves that it works, the variable is properly set:
cmd /c "set xx=yyyyyy & set xx & PAUSE"
output:
xx=yyyyyy
Other way to escape the %:
cmd /c "set xx=yyyyyy & ECHOxx.bat & PAUSE"
(ECHOxx.bat is a script which does echo %xx%)
output
yyyyyy
For the path, it's slightly complex as you noticed yourself, but it works fine too:
cmd /c "set PATH=%CD%\subdir;"%PATH%" & echoxx.bat"
it works: echoxx.bat had been moved in the subdir directory, and it is found.

How can I get a one liner xcopy command to run in vbscript?

I have an xcopy that cannot run correctly in a .bat (batch) file because of some Norwegian characters.
The line is:
xcopy /I /V /H /R /C /Y /K /O /X "G:\P - cad_files\Drawings\163997 Ø1000XØ90 T7-8-9.PDF" "F:\CAD\P - cad_files\Drawings\163997 Ø1000XØ90 T7-8-9.PDF"
If I run it from a batch file it fails as "0 files copied" because it translates the source file incorrectly due to the character set.
I've tried changing the charsets using chcp as well as using Notepad++ and encoding it with various charsets but it still fails due to a limitation I guess in batch processing in Windows.
So my question is, how can I get the line above to execute in a vbscript and record the xcopy output?
I tried:
set objShell = CreateObject("wscript.Shell")
strSource = "G:\P - Tdwos_cad_files\Drawings\163997 Ø1000XØ90 T7-8-9.PDF"
strDest = "F:\CAD\P - Tdwos_cad_files\Drawings\163997 Ø1000XØ90 T7-8-9.PDF"
command = "xcopy /I /V /H /R /C /Y /K /O /X " & chr(34) & strSource & chr(34) &chr(32) & chr(34) & strDest & chr(34) & " > F:\testvbs.log 2>&1"
objshell.run command
and then execute "cscript test.vbs"
But unfortunately this doesn't work despite the fact that if I change the objshell.run to wscript.echo it does show the proper output for the "command" variable...meaning it shows the right syntax.
So...any programmers know how I can properly run that original xcopy line in a vbscript? If you know how to run it in a batch file properly with the encoding, that's fine too...I just haven't figured that out yet.
Next script works with combined Norwegian-Czech folder and file names under under next conditions:
save the script UTF-16LE encoded with the 0xFFFE byte order mark, and
run the script as administrator!
Here's commented code snippet:
' save the script UTF-16LE encoded with the 0xFFFE byte order mark
' run the script as administrator!
' consider //U option: Use Unicode for redirected I/O from the console
' cscript //U 30767978.vbs
'
option explicit
dim objShell, strSource, strDest, command, cmd, xx
set objShell = CreateObject("wscript.Shell")
strSource = "D:\test\éíáýžřčšě\a Ø1000XØ90 b.txt"
strDest = "D:\test\ěščřžýáíé\a Ø1000XØ90 b.txt"
' chcp does not matter: `dir` as well as `xcopy` work for all
cmd = "%comspec% /U /C chcp 65000 & "
cmd = "%comspec% /U /C chcp 65001 & "
cmd = "%comspec% /U /C chcp 437 & "
cmd = "%comspec% /U /C chcp 852 & "
cmd = "%comspec% /U /C chcp 1250 & "
' chcp supposedly matters for redirected output
command = "dir " & """" & strSource & """ """ & strDest & """" & " & pause "
xx = objShell.Run( cmd & command, 1, true)
command = "xcopy /I /V /H /R /C /-Y /K /O /X " _
& """" & strSource & """ """ & strDest & """" & " & pause "
xx = objShell.Run( cmd & command, 1, true)
Wscript.Echo xx, cmd & command
While the accepted answer by JosefZ works well, I found that I could handle this much easier by simply taking my lines of xCopy and moving them from the .bat file to a Powershell .ps1 file and running it. Powershell doesn't balk at the character sets like Batch files do for some reason.
So I installed Powershell on the 2003 server and ran the script that way and it worked well.
I accepted JosefZ's answer since it answered the actual question, but posting this here in case others come across the same issue and want to utilize Powershell instead.

WScript v/s CScript

I have query related to this topic. Here is my script. I'm using below script to edit users on HP ILo board, it works fine, no error at all.
BUT here I can see cmd prompt, how do I run it in silent mode, i.e I do not want to see any cmd prompt, because I use BMC, so let it run in background, I will check the output later.
As you said I can simply change WScript to CSrcipt. But that does not work.
Any help please, please let me know where to modify.
Set wshShell = WScript.CreateObject ("WScript.Shell")
WshShell.Run "cmd.exe /v:on /k (set MYDIR=C:\Program Files\HP\hponcfg) & cd /d ""!MYDIR!"" & HPONCFG.exe /f Add_User1.xml /l log1.txt > output1.txt"
WScript.Sleep 1*60*1000
WshShell.Run "cmd.exe /v:on /k (set MYDIR=C:\Program Files\HP\hponcfg) & cd /d ""!MYDIR!"" & HPONCFG.exe /f Add_User2.xml /l log2.txt > output2.txt"
Set wshShell = Nothing
Wscript.quit
Regards
Use the second parameter of the .Run method
intWindowStyle Optional. Integer value indicating the appearance of
the program's window. Note that not all programs make use of this
information.
As I know nothing about BMC, I'd start with minimized (7, 6) before I'd try hidden (0).

CMD in vbscript

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.

Resources