0

I know how to run batch files without a window thanks to this question here: Run a .bat file in a scheduled task without a window.

The first answer is the one I am using. It states that to run a batch file without a window, create the following vbs file and run the vbs file instead.

Dim WinScriptHost
Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "C:\Scheduled Jobs\mybat.bat" & Chr(34), 0
Set WinScriptHost = Nothing

My question is whether or not it is possible to do this in just one single file. (somehow combining the vbs script and a batch file script) The answer above would require two files as the vbs file refers to a batch file. If it is possible, how would it be done?

3

In the script, create a temporary file and write the batch file into it. Then execute the script from the temporary file. Delete the temporary file when done.

Ideally, write the temporary file to a location where other users can't access it because you have a potential race condition there that could be used to elevate permissions.

1

There are many methods to create hybrid scripts documented at http://www.dostips.com/forum/viewtopic.php?f=3&t=5543.

I've listed a number of ways to create hybrid VBS/batch at https://stackoverflow.com/q/9074476/1012053.

However, it is a bit easier to create hybrid JScript/batch, so I will present that solution here.

Most hybrids execute the batch portion first, which then executes the VBS or JScript via CScript. But in your case you want to run the JScript first, which then runs the batch script. Note that the script must still have a .bat extension in order for the batch script to run. The JScript is run first via WScript with the //e:jscript option.

Here is a simple hybrid script that should do the trick:

@if (@X)==(@Y) @end /* harmless hybrid line that begins a JScript comment
::************ Batch portion ***********
@echo off

:: Your batch code goes here

exit /b
************ JScript portion ***********/
var objShell = new ActiveXObject("WScript.shell");
objShell.run(WScript.ScriptFullName,0);

The full command line for your scheduled task should look something like:

C:\Windows\System32\wscript.exe //e:jscript "C:\Scheduled Jobs\mybat.bat"

One particularly nice feature of this solution is you can run the batch script directly and it will perform normally within a visible cmd.exe window, or you can activate it via WSCRIPT for invisible execution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.