4

Hi I am having an issue passing parameters through a vbscript to a batch file. I am not very good at programming so I am sure I am just missing something stupid. Anyway I am using the vbscript to invisibly run the batch file

The vbscript is called as "C:\Program Files (x86)\scripts\check.vbs" %S "%D"

Where %S is state and %D is directory.

If I call the batch file directly as "C:\Program Files (x86)\scripts\checkdir.bat" %S "%D" everything works fine except there is a dos windows that pops up which is annoying.

So I looked around the internet and found this vbscript that can run a batch file silently. It does in fact run it silently however it does not pass the parameters to the batch file.

Here is the vbscript:

Set WshShell = CreateObject("WScript.Shell") WshShell.Run chr(34) & "C:\Program Files (x86)\scripts\checkdir.bat" & Chr(34), 0 Set WshShell = Nothing 

How would I go about passing the parameters? I have searched but I could not come up with anything that worked for me, I would either get an error on running it or the parameters would not pass.

2 Answers 2

4

Assuming you are calling the VBScript in this way...

wscript myscript.vbs ArgumentS ArgumentD 

Then swapping out line 2 for this should work...

WshShell.Run """C:\Program Files (x86)\scripts\checkdir.bat"" " & WScript.Arguments.Item(0) & " """ & WScript.Arguments.Item(1) & """", 0 

Note that I am assuming that you need to pass Argument D with double-quotes around it so I have included those. Inside a string VBScript will replace double-double quotes with single-double quotes. If it doesn't work, change WshShell.Run to wscript.echo and you should see exactly what VBScript is trying to pass. If it is not quite right, adjust it and try again with WshShell.Run.

Good luck.

Sign up to request clarification or add additional context in comments.

Comments

1

You need less luck, if you approach the problem of building complex command line in a more systematic fashion:

checkdir.bat:

@echo off echo '%1' '%2' > checkdir.log 

check.vbs:

Option Explicit Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject") Dim oWAU : Set oWAU = WScript.Arguments.Unnamed Dim oWS : Set oWS = CreateObject("WScript.Shell") Dim S : S = "state" If oWAU.Count >= 1 Then S = oWAU(0) Dim D : D = "directory" If oWAU.Count >= 2 Then D = oWAU(1) Dim sBFspec : sBFSpec = oFS.GetAbsolutePathName(".\checkdir.bat") ' One way of building a command from parts that need quoting ' Replacing placeholders in a template is another one ' Everything is better than concatenating, cf: ' """C:\Program Files (x86)\scripts\checkdir.bat"" " & WScript.Arguments.Item(0) & " """ & WScript.Arguments.Item(1) & """", 0 Dim sCmd : sCmd = Join(Array( _ qq(sBFSpec) _ , qq(S) _ , qq(D) _ )) ' sanity check WScript.Echo "About to call:" WScript.Echo sCmd WScript.Echo "Your last chance to check!" Dim iRet : iRet = oWS.Run(sCmd, 0, True) If 0 = iRet Then WScript.Echo "looks ok:" WScript.Echo oFS.OpenTextFile(".\checkdir.log").ReadAll() Else WScript.Echo "looks bad:", iRet End If WScript.Quit 0 Function qq(s) : qq = """" & s & """" : End Function 

Output:

cscript check.vbs About to call: "E:\trials\SoTrials\answers\16722567\vbs\checkdir.bat" "state" "directory" Your last chance to check! looks ok: '"state"' '"directory"' cscript check.vbs "i don't care" "c:\ \ \simsalabim" About to call: "E:\trials\SoTrials\answers\16722567\vbs\checkdir.bat" "i don't care" "c:\ \ \simsalabim" Your last chance to check! looks ok: '"i don't care"' '"c:\ \ \simsalabim"' 

or:

checkdir.bat "i don't care" "c:\ \ \simsalabim" type checkdir.log '"i don't care"' '"c:\ \ \simsalabim"' 

(The preserving of the " quotes came as a surprise to me)

1 Comment

Thank you this did not fix my issue but was a good learning exercise.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.