Simple CMD argument throught VBScript - vbscript

Its been a while...
I have the command line arg:
xml sel -t -v "computer/general/name" nsk1501901173m.xml > test.txt
that produces the results I want in the text document specified but I need to run it through a VBScript and for the life of me I cannot figure it out... any ideas?

You must use the WScript.Shell object to execute the app and the cmd /c (command shell with the /C parameter) before to pass your app arguments , check this sample
Set objShell = CreateObject("WScript.Shell")
objShell.run "cmd /c xml sel -t -v ""computer/general/name"" nsk1501901173m.xml > test.txt",1,true
Remember which script must be executed from the same location where the xml.exe app is located or even better add the location of xml.exe app to the PATH

Related

redirecting output from cmd /c

I have the following command (which I'm running from CScript but testing on the command line).
This command will consciously fail, but I want the output to go to the temp file radA9D4F.tmp
cmd /c java -jar "saxon9he.jar" -o:"radF45AB.tmp" -s:"WOnReportTemp_3816933937777_1.xml" -xsl:"filename.xsl1" > radA9D4F.tmp
but it simply print the error to the console (correct error but I want it to be redirected)
filename.xsl1 does not exist
and there is no redirected output in radA9D4F.tmp.
looking at https://ss64.com/nt/cmd.html it has a similar example
Spaces in Program Path + parameters with spaces:
CMD /k ""c:\batch files\demo.cmd" "Parameter 1 with space" "Parameter2 with space""
So I THINK I need to enclose the whole java piece in double quotes somehow like
cmd /c ""java -jar "saxon9he.jar" -o:"radF45AB.tmp" -s:"WOnReportTemp_3816933937777_1.xml" -xsl:"filename.xsl1""" > radA9D4F.tmp
but this just outputs
The system cannot find the path specified.
in fact even this
cmd /c ""java -jar "saxon9he.jar" ""
outputs
The system cannot find the path specified.
So I think the basic issue is how to run a command using cmd /c where potentially all paths may contain spaces

use root cmd window to execute commands in new cmd window

i'm trying to make a batch script for running my Java files. I've found out that there is no way to prevent auto-closure of a batch script(except using pause keyword, tho it just waits for key press). I've also discovered that starting a new window will cause only main windows to close, not the new one too so i want a way that the command SET /P file=Java file: is executed in the new window(I got the new window by using the start keyword. Is there any way to accomplish this without downloading other softwares? this is the code i came up with yet:
cd "C:\Users\DEVDHRITI\Desktop\Files&Folders\HMMMMM\programs\java programmes"
set /P file=Java file to execute:
java %file%^.jar
start
I guess you're looking for that :
cd "C:\Users\DEVDHRITI\Desktop\Files&Folders\HMMMMM\programs\java programmes"
start cmd /V:ON /K "#set /P "file=Java file to execute: " && java -jar !file!^.jar"
EDIT: Using expansion with /V and use of /K instead of /C to keep the cmd windows open.
Explanations : To launch a console process in another windows and keep it open after the end of this process console we launch another cmd process with the start command. We use /V:ON to use delayed expansion by default, meaning modified variables (like the one we prompt, %file%) will be expanded on the fly with ! (so !file! instead of %file%). We use /K to tell to this cmd process to not close when provided commands end. To this cmd process, we provide the following commands :
#set /P "file=Java file to execute: "
This one will ask for the jar filename (without extension) to launch.
The # means "do not echo the command itself on the console stdout" for a nice display.
java -jar %file%^.jar
This one launch the java interpreter (JVM) with the filename of a jar file to execute through the -jar parameter, filename build from the previous prompt and the .jar extension. The ^ here escapes the ., it seems not useful here but maybe your script/env requires it.
We link the both commands with && which means : _if left command from && is successful (it exits with ERRORLEVEL 0) then execute the right command from &&.

run 2 dos commands using vbScript as a single command

I am trying to change the directory and then run another command all
in 1 line. But for some strange reason I can't even get the first
command to work. I need help!!
What below code is does, first it launch cmd promt, change directory to directory where my jar file is situated ,that jar file take 3 arguments, all arguments are folder path so it enter java -jar myJar.jar C:\folder1 C:\folder2 C:\folder3
dim objShell
dim jarFileFolder, outFile, projFile,folderC ,cmd1,cmd2
'setting variuos paths
jarFileFolder ="C:\temp\"
'2 commands
cmd1= "cd jarFileFolder"
jarCommand = "java -jar myJar.jar C:\folder1 C:\folder2 C:\folder3"
set objshell = createobject("Wscript.shell")
objshell.Run "%compsec% /k cmd1 & jarCommand"
Below is the link to screenshot of the command i want to execute from vbscript
cmd to be executed from the cmd prompt
I'm writing this as an answer as I don't have enough rep to comment. You have a typo which might be causing the problem of the command not executing - compsec should be comspec.
The problem with your first command is that you are not using the jarFileFolder variable, but instead trying to change to a folder named "jarFileFolder". You should try:
'setting variuos paths
jarFileFolder ="C:\temp\"
'2 commands
cmd1= "cd " & jarFileFolder
And then when you run objShell:
set objshell = WScript.CreateObject ("WScript.Shell")
objshell.Run "%compsec% /k " & cmd1 & " " & jarCommand

vbs run command no output

Okay I created a small script to call "net file" and pump the output to a file. There are no spaces in the filenames, and everything seems okay when I run it interactively. So I run the following and I get no results and no errors:
set oWShell = CreateObject("WScript.Shell")
owshell.run "net file > C:\openfiles.txt"
Set owShell = nothing
Now if modify this just slightly to show execute the same command (and keep my command window open) it works just as expected (except it keeps the cmd window open which I can't have)
set oWShell = CreateObject("WScript.Shell")
owshell.run "%comspec% /k net file > C:\openfiles.txt"
Set owShell = nothing
It must be something obvious that I'm just missing. I only touch vbs once in a blue moon so it isn't something that I'm that used to using.
Any help would be appreciated! Thanks in advance!
You need a shell (i.e. %comspec%) to get shell features like redirection; the persisten window is what you asked for: the /k lets the shell stay open (try /c instead) and you should use the second and third parameter of the .Run method to get a hidden window (and possibly wait for the process before you zap the owShell).
Have a look at Run. Changing
owshell.run "%comspec% /k net file > C:\openfiles.txt"
to
owshell.run "%comspec% /c net file > C:\openfiles.txt", 0, FALSE
This will hide the cmd and carry on with the rest of the script... If you want to wait for the command to finish, change FALSE to TRUE.
Also keep in mind that some machines are funny about letting you write files directly to C:\. Possibly create a test folder and write to there instead!

How can I manipulate the command line through VBScript?

Could anyone give me example code to access the command line through VBScript? I want to execute two commands respectively in a command prompt.
For example, first command: cd C:\a, second command: winzip32.exe -min -a D:\d.
you can use wshell.Run. The other one is wshell.exec
Put your commands in a .cmd or .bat file and then run that. You could even build the batch file inside your script if it is not static.
MyBatch.cmd
CD C:\a
winzip32.exe -min -a D:\d
MyScript.vbs
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "MyBatch.cmd"

Resources