Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Run a batch script in background

91 views
Skip to first unread message

mattia

unread,
Aug 28, 2009, 6:17:07 PM8/28/09
to
I'm using the win scheduler to launch a certain task periodically.
Although there is no output to show to the user, the win shell is always
showed (for a fraction of a second). Is there a way to completely avoid
this?

As a remedy, I'm using a vbs script like:

Set WshShell = WScript.CreateObject("WScript.Shell")
obj = WshShell.Run("myprog.bat", 0)
set WshShell = Nothing

Is there a way to embed everything into the bat script?

Thanks, Mattia

Pegasus

unread,
Aug 28, 2009, 6:25:47 PM8/28/09
to

"mattia" <ger...@gmail.com> wrote in message
news:4a985763$0$43599$4faf...@reader1.news.tin.it...

Yes - create a dedicated account (e.g. "Scheduler"), then use this account
when scheduling the task. Remember to use a non-blank password!


T3X

unread,
Aug 28, 2009, 7:34:11 PM8/28/09
to
On Aug 28, 11:17 pm, mattia <ger...@gmail.com> wrote:
> Is there a way to embed everything into the bat script?

Yes, there is, but with vbscript it is ugly, because there are no
multiline comments. It's easier with jscript, see the example below.

Cheers,

Tomek

@if (1==0) @end /* no echo of this line
:: batch script starts here
@echo off
if [%1]==[doit] goto :doit
wscript -nologo -e:JavaScript %~sf0
goto :eof
:doit
echo Hello from cmd
pause
goto :eof */
// JScript starts here
WScript.Echo("Hello from js");
var oShell = new ActiveXObject("WScript.Shell");
oShell.Run("cmd /c \"" + WScript.ScriptFullName + "\" doit", 5, 1);

mattia

unread,
Aug 28, 2009, 7:49:28 PM8/28/09
to
Sounds quite cryptic to me... any explanation of the code please?

T3X

unread,
Aug 28, 2009, 8:09:44 PM8/28/09
to
On Aug 29, 12:49 am, mattia <ger...@gmail.com> wrote:
> Sounds quite cryptic to me... any explanation of the code please?

It's a batch file with embeded jscript (or jscript with an embeded
batch script, depending how you look at it :).

> > @if (1==0) @end /* no echo of this line :: batch script starts here

Here the trick is to use conditional compilation syntax of jscript to
suppress echo of the first line. Then we enter a multiline comment
that at the same time is the batch script you want to run.

> > @echo off
> > if [%1]==[doit] goto :doit
> > wscript -nologo -e:JavaScript %~sf0

Here's how to execute this script with wscript. You need to specify
the scripting engine explicitly, because the file will have .bat
(or .cmd) extension (otherwise cmd.exe wouldn't accept it).

> > goto :eof
> > :doit
> > echo Hello from cmd
> > pause
> > goto :eof */

Here the batch ends and jscript starts

> > // JScript starts here
> > WScript.Echo("Hello from js");
> > var oShell = new ActiveXObject("WScript.Shell"); oShell.Run("cmd /c \""
> > + WScript.ScriptFullName + "\" doit", 5, 1);

That's about it. What else do you want to know?

Cheers,

Tomek

mattia

unread,
Aug 28, 2009, 9:07:11 PM8/28/09
to
wscript -nologo -e:JavaScript %~sf0
What %~sf0 stands for?
Can you point me some good tutorial about jscript? Is it common practice
to mix batch and jscript? I mean, probably bat is perfect for executing
commands and jscript can be used to e.g. show some popup messages (apart
obviously doing lot more)? Do you regular use it?

Thanks, Mattia

T3X

unread,
Aug 29, 2009, 11:34:20 AM8/29/09
to
On Aug 29, 2:07 am, mattia <ger...@gmail.com> wrote:
> wscript -nologo -e:JavaScript %~sf0
> What %~sf0 stands for?

Full path to the executing batch file in 8.3 format. See call /? and
for /? for more details on parameter expansion.

> Can you point me some good tutorial about jscript?

You can try msdn for documentation and sample code. I rarely use
jscript myself.

> Is it common practice to mix batch and jscript?

It is more common to dynamically write one script from another and
execute it. But I've seen e.g. perl scripts embedded in .bat files, so
it happens.

> I mean, probably bat is perfect for executing
> commands and jscript can be used to e.g. show some popup messages

For that you can as well create vb or jscript dynamically from a batch
file, as I mentioned above.

> Do you regular use it?

No. I think it is quite obfuscated. I only gave you this example,
because you asked for it. This approach makes sense only if you
require a single file solution that doesn't create any intermediate
files.

Cheers,

Tomek

0 new messages