Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows script - run silent but wait for completion / return right code

Here's my situation:

  • I have a BAT file that takes a long time to run (1minute to 70 minutes)
  • I schedule it with Windows scheduler to run every 10 minutes
  • If it schedules again while it is still running, nothing happens (this is good)

My problem is that I need my BAT to run silently, and it doesn't. So, I want to launch it with a Windows script like the following:

Set WshShell = CreateObject("WScript.Shell") 
WshShell.Run chr(34) & "C:\Batch Files\syncfiles.bat" & Chr(34), 0
Set WshShell = Nothing

Unfortunately, when I schedule this script, it does the job but returns instantly, making Windows scheduler think the task is finished when in reality the BAT is just running off by itself somewhere.

Because of this, Windows will reschedule the job again 10 minutes later and make multiple run.

What I need:

Is there a way to tell the Windows script file to wait for the target of the .Run command to complete before progressing/exiting? Basically, I want it to act like I launched another thread and then called join on it, so it does the same thing the BAT would have, but without displaying the console window.

Other Solutions

Tell me any other way to get this BAT to execute silently (powershell commands, whatever) and I'll accept it as a solution as well. Just don't tell me to write a full on C++/C# appliation around it, that's overkill :)


Running: Windows Server 2008 R2

like image 839
John Humphreys Avatar asked Dec 27 '22 20:12

John Humphreys


2 Answers

I think all you need is TRUE for the optional 3rd argument

WshShell.Run chr(34) & "C:\Batch Files\syncfiles.bat" & Chr(34), 0, TRUE
like image 111
dbenham Avatar answered Jan 14 '23 13:01

dbenham


Here goes the madness...

Create a file invisible.vbs:

CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, True

Then in the task scheduler, call your script like this:

wscript.exe "C:\Batch Files\invisible.vbs" "C:\Batch Files\syncfiles.bat"

I tested it and this does really work: No command window, and the task is considered running until the batch script ends.

Credit goes to https://superuser.com/a/62646/40362.


For 2008 R2 make invisible.vbs have this content, and just execute it directly. It will run the BAT silently and wait for completion.

Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "D:\IUpdateScript.bat", 0, true
like image 42
marapet Avatar answered Jan 14 '23 14:01

marapet