Search code examples
batch-filevbscriptjscript

Launch a Java/Batch hybrid without displaying command prompt


A little while I asked a question (and got a brilliant response) to create the program below, what I'd like to is silently launch it, so the command prompt does not display in the background. The program below tests for "Agent.exe" and if it finds it, displays a message box to the user telling them the program will close in 2 minutes unless they press ok.

now

I came across this VBS script

Set WshShell = CreateObject("WScript.Shell") 
WshShell.Run chr(34) & "Program goes here" & Chr(34), 0
Set WshShell = Nothing

but have not the tiniest clue to combine it with my script

@if (@CodeSection == @Batch) @then
setlocal
for /f %%I in ('forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo 0x07"') do set "beep=%%I"
set /P "=%beep%"<NUL
set /P "=%beep%"<NUL
set /P "=%beep%"<NUL
setlocal
set "task=agent.exe"
set "timeout=120"
rem // Is %task% running?
tasklist /fi "imagename eq %task%" | find /i "%task%" >NUL && (

    rem // Re-launch script with JScript interpreter
    wscript /e:JScript /nologo "%~f0" %timeout% || (
        rem // If timeout or user hits No, kill %task%
        taskkill /im "%task%" /f
    )
)

rem // End main runtime

goto :EOF

rem // Begin JScript portion
@end
var osh = WSH.CreateObject('WScript.Shell'),
    nag = 'Greetings!  Your administrator has requested you to log out of Program '
        + 'after work.  It appears you are still using it.'
        + ' If you are still here, Press Yes to continue working.\n\n'
        + 'Otherwise, press no to close Program.'
        + 'Program will close automatically in less than ' + WSH.Arguments(0) + ' seconds.';
    popup = osh.Popup(nag, WSH.Arguments(0), 'Are you still here?', 0x4 + 0x20 + 0x1000);
WSH.Quit(popup - 6);

Anyone have any ideas? (shoutout to Rojo, who answered my initial Question with the above script)


Solution

  • To answer your question, that VBScript snippet will work. Just save it with a .vbs extension and replace "Program goes here" with the path + filename of your batch script. When you double-click the vbs file or execute it with wscript /nologo "path\to\vbsfile", it will run the batch script hidden (no console window) but the popup will still appear.


    If you'd prefer to keep all your code within a single file, you could invoke powershell -windowstyle hidden to hide the window. You'll still see the black window for a second, but it'll disappear before the popup appears.

    @if (@CodeSection == @Batch) @then
    setlocal
    
    rem // hide current window
    powershell -windowstyle hidden -command ""
    
    for /f %%I in ('forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo 0x07"') do set "beep=%%I"
    set /P "=%beep%"<NUL
    set /P "=%beep%"<NUL
    set /P "=%beep%"<NUL
    
    set "task=agent.exe"
    set "timeout=120"
    rem // Is %task% running?
    tasklist /fi "imagename eq %task%" | find /i "%task%" >NUL && (
    
        rem // Re-launch script with JScript interpreter
        wscript /e:JScript /nologo "%~f0" %timeout% || (
            rem // If timeout or user hits No, kill %task%
            taskkill /im "%task%" /f
        )
    )
    
    rem // if not in a self-destructing window, re-show
    set "caller=%cmdcmdline:"=%"
    if /I not "%caller:~0,6%"=="cmd /c" powershell -windowstyle normal -command ""
    
    rem // End main runtime
    goto :EOF
    
    rem // Begin JScript portion
    @end
    var osh = WSH.CreateObject('WScript.Shell'),
        nag = 'Greetings!  Your administrator has requested you to log out of the Cisco '
            + 'Agent Desktop after work.  It appears you are still using it.  If you '
            + 'are still here, Press Yes to continue working.\n\n'
            + 'Otherwise, press No to close the agent.  Agent will close automatically '
            + 'in less than ' + WSH.Arguments(0) + ' seconds.',
    
    popup = osh.Popup(nag, WSH.Arguments(0), 'Are you still here?', 0x4 + 0x20 + 0x1000);
    
    WSH.Quit(popup - 6);
    

    Shout received and appreciated. I still think you ought to play a cheesy midi file instead of beeping. :)