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
Yes - create a dedicated account (e.g. "Scheduler"), then use this account
when scheduling the task. Remember to use a non-blank password!
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);
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
Thanks, Mattia
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