Run batch files silently using a Scheduled TaskThe Task Scheduler is one of them. This feature allows you to run tasks in the background, periodically or every day. You can easily schedule a Batch file to run automatically using Scheduled Task with options available out of the box.
The most simple solution is to run the batch file minimized. The batch file will still be visible in the task bar while running. For batch file started by a shortcut, this may be "sufficiently hidden".
Here is a possible solution:
From your first script, call your second script with the following line:
wscript.exe invis.vbs run.bat %*
Actually, you are calling a vbs script with:
%*
)Then, invis.vbs will call your script with the Windows Script Host Run() method, which takes:
Here is invis.vbs:
set args = WScript.Arguments
num = args.Count
if num = 0 then
WScript.Echo "Usage: [CScript | WScript] invis.vbs aScript.bat <some script arguments>"
WScript.Quit 1
end if
sargs = ""
if num > 1 then
sargs = " "
for k = 1 to num - 1
anArg = args.Item(k)
sargs = sargs & anArg & " "
next
end if
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run """" & WScript.Arguments(0) & """" & sargs, 0, False
Do you need the second batch file to run asynchronously? Typically one batch file runs another synchronously with the call
command, and the second one would share the first one's window.
You can use start /b
second.bat to launch a second batch file asynchronously from your first that shares your first one's window. If both batch files write to the console simultaneously, the output will be overlapped and probably indecipherable. Also, you'll want to put an exit
command at the end of your second batch file, or you'll be within a second cmd
shell once everything is done.
Convert the batch file to an exe. Try Bat To Exe Converter or Online Bat To Exe Converter, and choose the option to run it as a ghost application, i.e. no window.
I think this is the easiest and shortest solution to running a batch file without opening the DOS window, it can be very distracting when you want to schedule a set of commands to run periodically, so the DOS window keeps poping up, here is your solution. Use a VBS Script to call the batch file ...
Set WshShell = CreateObject("WScript.Shell" )
WshShell.Run chr(34) & "C:\Batch Files\ mycommands.bat" & Chr(34), 0
Set WshShell = Nothing
Copy the lines above to an editor and save the file with .VBS extension. Edit the .BAT file name and path accordingly.
For self-hiding you can use getCmdPID.bat and windowMode.bat:
@echo off
echo --- self hiding bat ----
pause
call getCmdPid.bat
set PID=%errorlevel%
call windowMode.bat -pid %PID% -mode hidden
Here's my collection of ways to achieve that - and even more - where it was possible I've tried to return also the PID of the started process (all linked scripts can be downloaded and saved with whatever name you find convenient):
Example usage:
call IEXPhidden.bat "cmd /c myBat.bat" "argument"
Example usage:
call SCHPhidden.bat "cmd /c myBat.bat" "argument"
Example usage (for more info print the help with '-h'):
call ShellRunJS.bat "notepad.exe" -style 0 -wait no
Example usage (for more info print the help with '-h'):
call win32process.bat "notepad" -arguments "/A openFile.txt" -showWindows 0 -title "notepad"
Example usage (for more info print the help with '-h'):
call ProcessStartJS.bat "notepad" -arguments "/A openFile.txt" -style Hidden -directory "." -title "notepad" -priority Normal
In the other question I suggested autoexnt. That is also possible in this situation. Just set the service to run manually (ie not automatic at startup). When you want to run your batch, modify the autoexnt.bat file to call the batch file you want, and start the autoexnt service.
The batchfile to start this, can look like this (untested):
echo call c:\path\to\batch.cmd %* > c:\windows\system32\autoexnt.bat
net start autoexnt
Note that batch files started this way run as the system user, which means you do not have access to network shares automatically. But you can use net use to connect to a remote server.
You have to download the Windows 2003 Resource Kit to get it. The Resource Kit can also be installed on other versions of windows, like Windows XP.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With