1.

Solve : Batch file passing parameters??

Answer»

I okay I'm completely new to batch files so I need some help. I've been reading almost all the tutorials and FAQ that I've seen, however i cant find one that will answer my question.
I want to call a batch file from a Vb Script function.
I want the batch file to write some lines of text to a textfile and that's it, however the lines of text will be determined from the script. Is it possible to pass parameters to a batch file? From everything I've read it seems like it's not possible.
All help appreciatedPassing parameters to a batch file is very possible:

This is as simple as it gets:

Code: [Select]@echo off
for /l %%X in (1,1,%1) do echo This line is output >> output.txt

When you run the file, pass how many lines you want on the command line. For instance, if the name of the file is mybatch, running mybatch 7 will write 7 lines to the output file.

You can generate the count in your VBScript and pass it along when you run the batch file. If you need help with the VBScript, get back to us.

thanks sidewinder...however it's not the answer I'm looking for. I would like to pass to the batch file strings of text as parameters, right now I'm looking at just using the openTextFile method in the script to write to the file, after the batch file has created it.
I would like to pass the string of text as parameters to the batch, as that would streamline the process in a single call.??
Sorry about my original post it was a little too vague. thanks.
If the string contains just one word you can pass the string as is, if the string contains embedded spaces you can pass strings of text provided they are quoted. Do not confuse this with the quotes needed in VBScript for strings. You need to pass double quotes [Chr(34)] as part of the string. When the quoted string arrives in the batch file, you can either strip the quotes or insert them into the file as is:

Strip the quotes:

echo %~1 > output.txt

or

Keep the quotes:

echo %1 > output.txt

Quote

right now I'm looking at just using the openTextFile method in the script to write to the file, after the batch file has created it.

I'm baffled. Maybe I'm just misreading this, but you're saying the batch file will write a file and then the script will also write to it?

Why keeping shelling out to the cmd processor to create a file? Can't you use the script by opening a text file forwriting and using the FileSystemObject Writeline method?


Thanks that what I was looking for!!

EXCEPT how I am suppose to pass down the quotations as parameters when i;m calling the batch FORM the script.

this is what I have so far in the script I get all the correct colors that represent dim's and strings but

(batchFileFolder) is a var that hold the path to the where the batch file is located
Code: [Select]Set WShShell = CreateObject("WScript.Shell")
dim name
name ="carter"
dim hello
hello ="Hello_World"
dim number
number = "5344"
RetCode = WShShell.Run("""" & batchFileFolder & "myBatch.bat" &" "& name &" "& hello &" "& number"")

thanksPutting quotes around strings that are quoted parameters to methods is enough to drive one to drink, so I grabbed a beer and did it the easy way.

Code: [Select]strRun = batchFileFolder & "myBatch.bat " & """" & name & " " & hello & " " & number & """"
WshShell.Run(strRun)

When it enters the batch world it LOOKS like:

...\mybatch.bat "Carter Hello_World 5344"



batchFileFolder needs to end with a backslash. One parameter is passed to the batch file as a quoted string. Use the method in the previous posts to keep or drop the quotes. I suggest you put a path on the output file in the batch code; you never know what directory the cmd window will default to.

Correction to previous post: Use >> instead of >

Thanks, for the help... really appreciate it.. Actually one last question?

what could possibly prevent a batch file from executing when called from a script?

b/c i'm actually calling a second batch from the same script, but the one that you helped me with is falling to run. however I still get a return code of zero??? but with no output file? could this then be a problem in the batch and not in the script
thanks-Did the batch file actually fail to run or can you not find the output? I believe the default directory for the cmd processor is the %userprofile% value. You can override this by pointing the path of the output file in the batch code.

If you could post the contents of the batch file and the console output of the batch file run, perhaps we can see where to GO next. If you're using echo to create your file, it's difficult to imagine not getting a zero return code.

Quote
this is what I have so far in the script I get all the correct colors that represent dim's and strings

If you have some sort of a VBScript editor you may be able to step through the code and see what is happening.

this is is contents of the batch file...

Code: [Select]@echo off

echo %~1>"C:\Inetpub\buildInstaller\batchFiles\output.txt"
echo %~2>>"C:\Inetpub\buildInstaller\batchFiles\output.txt"
echo %~3>>"C:\Inetpub\buildInstaller\batchFiles\output.txt"
The output that I mentioned was just response.write(retCode) the second batch file that I was comparing it too was contained in the same directory and was outputting to the same destination was as this was. Both batch files were located in this directory and were outputting to the same one. this batch works fine when I call it from the START>RUN dialog just not when I call form the script.
The second script is using the same WScript object and is being called after this one. thanksIt might help to know what the second batch file is doing. There are parameters to the run method which might help:

Code: [Select]object.Run(strCommand, [intWindowStyle], [bWaitOnReturn])

If the second batch file needs the output of the first batch file, it may be necessary to set bWaitOnReturn to True:

Code: [Select]strRun = batchFileFolder & "myBatch.bat " & """" & name & " " & hello & " " & number & """"
WshShell.Run(strRun,,True)

I'm still unsure why you're using batch code especially now that it appears this is ASP code. I'm also confused by the %2 and %3 parameters. The quotes indicate only one parameter was passed (%1).

It might be interesting to see the console log of the batch file.






Discussion

No Comment Found