Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bat file running in slient mode 2

Status
Not open for further replies.

Phil92

Programmer
Nov 23, 2001
17
FR
Good morning,

Does somebody know how to run a .bat file in "silent mode" ?

The .bat file makes some files copy, it is launched from an application, and I would like to avoid the (short) DOS window that appear while the scrit is executing.

TIA

Phil
 
Hello Phil92,

My solution is to launch the batch file via a transcient .vbs. Suppose the batch is abc.bat, then make a text file abc.vbs (actually, any name with .vbs extension) with the following lines.
Code:
filespec="d:\123\abc.bat"   '< edit the location & name
createobject("wscript.shell").run chr(34) & filespec & chr(34), 0, true
regards - tsuji
 
Correction:

I should have put false (or blank) instead of true up there.
Code:
filespec="d:\123\abc.bat"   '< edit the location & name
createobject("wscript.shell").run chr(34) & filespec & chr(34), 0, [COLOR=red]false[/color]
- tsuji
 
Thank you. I will try this.

Do you know if it would works on Windows XP as well ?
And what about Windows 98 ?

I was hoping for a secret parameter (/s like silent...) to do the job... Are you sure that there is not any ?

Thanks you very much for you help.

Phil
 
Phil92,

It would on xp down to 9x. Only if you run it (double click on the .vbs) on 9x, you should check if the free windows script host been installed. Verification is two-click away though. If it does not run (through default file association), try download it from ms. It's free.

- tsuji
 
Fine, it works without the command window appearinf even briefly.

but (there is a but...), I need to pass a parameter to my .bat file. Is it possible to do it throught the .vbs file ?

Thank you for your help,

Phil
 
Phil92,

[1] It serves as a launcher. Passing the variable to the batch is undisturbed if it is hard coded, working with double-click or called in another batch, then just replace the filespec with the full commandline including the arguments.
Code:
filespec="d:\123\abc.bat"
arg="ibn\jsmith 1a2b3c"
createobject("wscript.shell").run chr(34) & filespec & arg & chr(34), 0, false
You get the idea.
[2] But if you want to do it at interactive command prompt:
c:\>cscript.exe d:\123\abc.vbs ibn\jsmith 1a2b3c
then three more lines.
Code:
filespec="d:\123\abc.bat"
for i=0 to wscript.arguments.count-1
    arg=arg & " " & wscript.arguments(i)
next
createobject("wscript.shell").run chr(34) & filespec & arg & chr(34), 0, false
- tsuji
 
correction (again!):

I need a hardspace as separator in [1] between arguments and executable. But in order to preservse similarity between [1] & [2], in the section [1] we can just add a space to arg before passing to run.
Code:
arg = [COLOR=red]" "[/color] & arg
- tsuji
 
tsuji,

If you could, please rewrite the final solution, rather than "patch" it.

I would appreciate it.
 
bcastner, yes you're right.

Here is the [1] ammended.
Code:
filespec="d:\123\abc.bat"
arg="ibn\jsmith 1a2b3c"
[COLOR=green]arg=" " & arg[/color]
createobject("wscript.shell").run chr(34) & filespec & arg & chr(34), 0, false
- tsuji
 
Addenda:

In order to eliminate another shortcoming which may sooner or later turn up when using this way of doing thing, namely, the space in the long filename. I re-amend [1] and [2] for cases with arguments. Sorry for the confusion! Would prefer embarrassment than deficient solution.

[1] Hard coded arguments
Code:
filespec="d:\123\abc.bat"
arg="ibn\jsmith 1a2b3c"
arg=" " & arg
createobject("wscript.shell").run chr(34) & filespec & chr(34) & arg, 0, false
[2] Interactive arguments
Code:
filespec="d:\123\abc.bat"
for i=0 to wscript.arguments.count-1
    arg=arg & " " & wscript.arguments(i)
next
createobject("wscript.shell").run chr(34) & filespec & chr(34) & arg, 0, false
- tsuji
 
tsuji,

Thank you.

You had made the point earlier " need a hardspace as separator in [1] between arguments and executable. But in order to preservse similarity between [1] & [2], in the section [1] we can just add a space to arg before passing to run."

And, with the flag value "false" it makes it clearer.

Thank you.

Bill Castner

 
Great !

It works... from the command line.

Running a vbs file makes my antivirus software to prompt for asking if the script is allowed.

The VBS file does not run from my application, even if I disable the antivirus software. Should I put a "start" command or something like that to make it run ?

Thank you again

Phil
 
Give permissions for WSH in your antivirus (likely your firewall software as well).

Then in the logon script:
cscript.exe filename.vbs
 
But if I use cscript.exe, the window command is opened while it runs... It was what I was trying to avoid.
Maybe I miss something.

Phil
 
Phil92,

This is going to be quite delicate. I should have brought it up earlier too because it involves memory leaks and orphan processes.

[A] Close cmdprompt windows
You know, in a batch file, in case you run it by double-click or start|run, if you want the dvm window to close after the batch run its course, you have two basic choices.
[A.1] Run it via a shortcut (.pif) with the close after finished checkbox checked.
or
[A.2] You modify the batch file. You keep all the instructions you want it to run. But, at the end of it, you add the three-line ritual:
Code:
cls
@echo off
exit
With this modified batch file, it will close itself after it's run its course.

Now, with the above in mind. To launch a long-running batch with hidden dvm window, you need to take care of its being close by itself. Else, you would discover that a winoa386.mod process will survive the batch process---this is undersirable.

You have to prepare the batch file. If you add the three-line ritual to its end, then the solution proposed above is safe. Otherwise, you have to kill the winoa386.mod blindly and indiscriminately. I sometimes use
Code:
kill /f winoa386.mod
in batch or from commandline (instead of top-right corner X), with kill.exe utility.

If you use the shortcut method as discussed in [A.1], then, you can actual run the .pif as executable. In that case, you have the shortcut prepared. In the .vbs file you run the filespec with an additional .pif there by convention.
Code:
filespec="d:\123\abc.bat"
filespec=filespec & ".pif"
'etc...
This will sort the problem but very clumsily because you need to make out a pif and check the checkbox control.

In your application, if you must run a batch file, then this launching batch file can be constructed like this.
Code:
::the vbs is at d:\123\abc.vbs with arguments
cscript.exe d:\123\abc.vbs //B [[arg1] [arg2] ...]
cls
@echo off
exit
Then, you will have the window brought up by the cscript line auto-close. (This is the response to the issue you raise above.)

You see, it all becomes quite confusing as the combinations are many depending on what you want and where you launch. Feel free to see what you can make out of it.

- tsuji
 
Many thanks for your kind help.

My DOS window is closing by itself so the (small) bad effect is just a flicking window while my .bat file is running. I would have liked to avoid this but if it is not easy or possible this is not a big problem.

Thanks again

Phil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top