run 2 dos commands using vbScript as a single command - vbscript

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

Related

Failure to run VBS script with task scheduler

we have this vbs script we use to update certain documents with SyncToy.
This is the script as it is currently written:
'--------------------------------------------------
Set oShell = CreateObject("WScript.Shell")
sFilePath = chr(34) & "C:\Program Files\SyncToy 2.1\SyncToyCmd.exe" &
chr(34) & "-R"
iRC = oShell.Run(sFilePath, 0, True)
' Return with the same errorlevel as the batch file had
Wscript.Quit iRC
'---------------------------------------------------
I didn't write this script, and I have very little experience with scripting.
I have a task set up in task scheduler that runs this script anytime the device connects to a network. The script should run SyncToy and then synchronize the folder pair that is set up. I have tried running the script through command prompt with the cscript command but nothing happens as far as I can tell. At least the folders aren't syncing.
The script is running on a Windows 10 pro tablet
I have verified that the task is indeed running when it is supposed to. I'm just not sure if it is an issue with the way the script is written or if the task settings need to be changed. Is there anything wrong with the script as far as you can tell?
I was unsure whether to post this here or over in serverfault. If this doesn't belong here please move the question over to serverfault
Update: I've verified that this isn't a problem with the script. This problem apparently arose only after the update from SyncToy 2.0 to 2.1.
Thanks Guys.
There is a error with the sFilePath lines.
First, I don't know if this was originaly on a single line but it should (or add "_" before changing line).
Then, this (...)& >"-R" would not work. The ">" symbole is outside the quotes and generate a error.
If you want to execute this command: "C:\Program Files\SyncToy 2.1\SyncToyCmd.exe" -R,
this is the way to do this:
sFilePath = chr(34) & "C:\Program Files\SyncToy 2.1\SyncToyCmd.exe" & chr(34) & " -R"
You can also add msgbox sFilePath to show a popup with the value of sFilePath.
To test/run the script, you just need to double-click on it.

VB Using "Runas"

I am having a heck of a time getting some syntax correct here.
I have a small VB Script which prompts for credentials and then uses those credentials to run another vb script:
set objShell = WScript.CreateObject("WScript.Shell")
strAdminName = inputBox("What is your username (Domain\Username)")
objShell.Run "runas /user:" & strAdminName & " ""Wscript.exe \\xxx.xx.xxx\dfs\Tumw-IS\Juniper Tools and Utilities\Juniper Removal Tools\delete_folders.vbs"" "
When I run the script everything works, except it can't find the script I am calling. I get an error stating "There is no file extension in "\xxx.xx.xxx\dfs\tumw-is\Juniper"
Obviously the problem is in the fact that there are spaces in the file location and also that I am using a DFS link. When I run this and target a VBS file with no spaces in the location it works fine.
I just can't get the syntax down to handle the DFS link or the spaces in the name. I assume it's pretty straightforward, but I just can't get it to work.
Any ideas?
Thanks
-John
Try this:
objShell.Run "runas /user:" & strAdminName & " ""Wscript.exe \""\\xxx.xx.xxx\dfs\Tumw-IS\Juniper Tools and Utilities\Juniper Removal Tools\delete_folders.vbs\"""""

Simple CMD argument throught 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

Get full path of VBScript script file when WScript not available

I'd like to get the full path to the script file I am executing. A quick google search shows me that WScript.ScriptFullName will do the trick. The problem is that I am executing this script outside of the Windows Script Host (I'm using ScriptUnit) and so if I try using that code I get the error:
Variable is undefined: 'wscript'
How do I get the full path to the current script file if I can't use WScript?
Can't you shell out, start a small script with cscript and pass the value to the parentscript throug an environmentvariable ? Can't test this since i don't have or use ScriptUnit.
Obviously the following would not work sincve you don't have Wscript with you but has ScriptUnit a similar feature to read environmentvars ?
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "CScript.exe " & """" & ScriptName & """" 'this would set your environmentvar scriptpath
Set objProcessEnv = WshShell.Environment("Process")
path = objProcessEnv("scriptpath")
Or start the script from a batch or other script, record the path in a text-file and read the contents of that file in your mainscript.

VB script want to run .bat file from VB

I am try to run the go.bat from VB but when I run the script I get: :cant find specific file
but from the cmd window the file go.bat exist. what the problem?
Dim MyShell
Dim shell_cmd
shell_cmd = "C:\Program Files\dir1\dir2\wizard\go.bat"
set MyShell = CreateObject("WScript.Shell")
MyShell.Run shell_cmd, 1, 1
from cmd window
C:\Program Files\dir1\dir2\wizard>go.bat
Your batch file's full path contains spaces, so you need to enclose it in double quotes, like this:
shell_cmd = """C:\Program Files\dir1\dir2\wizard\go.bat"""
or
shell_cmd = Chr(34) & "C:\Program Files\dir1\dir2\wizard\go.bat" & Chr(34)
not sure if you knew but in vb you can use the Shell function:
http://msdn.microsoft.com/en-us/library/xe736fyk(VS.71).aspx
(seems easier than what you're using)
I don't know why you get this message, but the two paths you mention are in fact different:
C:\Program Files\dir1\dir2\wizard\go.bat
C:\Program Files\dir1\dir2\wizard>go.bat
^

Resources