Check if a process is closed/not running, then restart the application - batch-file

Below is the script I am using to detect if my application is being closed. If it is closed the application will restart again. The application should run in the background as I am running this on startup.
run.bat
#echo off
:Restart
start "AppNAME" /wait "C:\Program Files (x86)\App\App.exe"
goto Restart
How can I hide the command windows after running the .bat file?

As per your requirement here is a vbs file that will run the batch file in background.just past the vbs file in startup and the batch file somewhere else.
REM 0 = hide window, 1 = show window (useful for debugging)
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run """" & "file.bat" & """" & sargs, 0, False
Set WshShell = Nothing

Related

Unable to invoke multiple batch files

I have VBScript file as follows which launches multiple batch files.
Invoke.vbs
const RunbatchtLocation = "C:\Temp"
Set shell = WScript.CreateObject("WScript.Shell")
shell.run RunbatchtLocation & "\Launch.bat",1,True
Launch.bat
start cmd /k "C:\Temp\All10.bat"
start cmd /k "C:\Temp\30.bat"
C:\Temp\50.bat
The above file able will executed All10.bat , 30.bat and 50.bat which is work fine.
My problem is here in All10.bat which is as follows:
CALL "c:\temp\10-1.bat"
CALL "c:\temp\10-2.bat"
CALL "c:\temp\10-3.bat"
It starts with 10-1.bat and is able to execute all the specified files, but after it completes it stops and never executes 10-2.bat or 10-3.bat.
Will someone tell me where I am going wrong?

How to hide a command prompt while running a batch file in java

Okay so I am making a game and have fixed most of my problems there is just one thing, the cmd prompt is annoying me when it pops up. If you have any recommendations on how to have the prompts functions run without it opening the prompt itself.
This is an answer from #badbod99 for the question How can I run a windows batch file but hide the command window?:
Const HIDDEN_WINDOW = 12
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objStartup = objWMIService.Get("Win32_ProcessStartup")
Set objConfig = objStartup.SpawnInstance_
objConfig.ShowWindow = HIDDEN_WINDOW
Set objProcess = GetObject("winmgmts:root\cimv2:Win32_Process")
errReturn = objProcess.Create("mybatch.bat", null, objConfig, intProcessID)
In the last line, change myfile.bat to the file location of your batch file. Save this as a .vbs file and run it instead of the batch file.
The batch file part:
#echo off
cls
start "" "C:\directory\yourfile.java"
exit
Save that as a .bat file. This is the batch file that will be run by the .vbs file.

Running master bat file in invisible mode

I have a master.bat file which has...
call file1.bat
call file2.bat
call file3.bat
call file4.bat
I want to schedule it on my Windows server 2008 to run in silent/invisible mode.I'm looking for some way to run this master.bat without anything visible to the user (no window, CMD interface ,no taskbar name etc..)
I don't want to install any batch to exe software.
I tried by changing the User running the task to "SYSTEM" and it has the work done but I can't do this in actual.
I have found that Windows Script Host’s Run Method allows you to run a script in invisible mode as.....
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\Batch Files\master.bat" & Chr(34), 0
Set WshShell = Nothing
but no more file please :) any other suggestion for this.
EDIT1
Considering the limited options available..it would be OK to use Windows Script Host’s Run Method,but how i can schedule master.vbs in task scheduler.. ?
For an extended view of it, check for hybrid batch / vbscript / javascript files here in stackoverflow.
Save this as master.cmd and adapt as needed.
#if (#This==#IsBatch) #then
#echo off
rem **** batch zone *********************************************************
rem Check if started from javascript part of script.
rem We are checking an environment variable set from javascript part.
if "%_run_hidden_%"=="true" (
goto startBatchWork
)
rem if not started from javascript, call javascript part to restart batch.
wscript //E:JScript "%~dpnx0"
exit /b
:startBatchWork
rem Here starts the real work of the batch file
msg %username% "Batch file running hidden"
rem End of batch area. Ensure batch ends execution before reaching
rem javascript zone
exit /b
#end
// **** Javascript zone *****************************************************
// Instantiate the needed component to interact with Shell
var shell = WScript.CreateObject('WScript.Shell');
// Set the environment variable that the batch part will check to know
// it's running hidden
shell.Environment('Process').Item('_run_hidden_')='true';
// start the batch part of the script calling %comspec% with the current
// script as parameter, hidden (0) and not waiting for it to end (false)
shell.Run('"' + shell.ExpandEnvironmentStrings('%comspec%') + '" /c "' + WScript.ScriptFullName + '"', 0, false );
// All done. Exit
WScript.Quit(0);
CMDOW is a tool that will allow the batch to run hidden.
It is flagged as a hack tool by various AV programs.

WshShell.run and major problems in Windows 7

This is a VBScript that I would like to improve. I would like four things :
1) Add a line that would rename the extension cleanup.dll to cleanup.exe, so as it can be called by the WshShell.run and executed (hidden).
2) The way it is written just below, the script opens two screens : the screen of the cleanup.exe and a blank screen, which should be hidden for the user and it is not what is happening ! How to hide the second screen ? I want to run it invisibly (the user cannot close or manipulate the second screen. It will be closed via code that is inside the cleanup.exe).**NOTE : The code below works perfectly in Windows XP, but not on Windows 7. How to make it work in all Windows platforms ?
VBSCRIPT "Second.vbs"
Set WshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFile "cleanup.dll" , "cleanup.exe"
WshShell.Run "c:\cleanup.exe", 0, TRUE
Set WshShell = Nothing
BATCH "Master.bat"
#echo off
wscript Second.vbs
exit /b
3) Is there a good and reliable software to convert from VBS to EXE ?
4) The other problem I am having is that the command line below does not yield results.Must I use the second pard of the code below instead ? Why ??
Suppose that my Batch file is in located in drive f:\
If I double click on it, my screen should be then populated with information extracted from the TXT file, which actually resides in drive c:\
#echo off
set DRV=C:\August\MyProgram
cd\
cd %DRV%
type test.txt & pause>nul
#echo off
set DRV=C:\August\MyProgram
cd\
c:
cd %DRV%
type test.txt & pause>nul
Thank you in advance for the explanations and solutions
Why run with batch, vbscript is more powerfull and offers more controll.
about the visible console window
WshShell.Run "c:\cleanup.exe", 0, TRUE should hide the console while running and waits before continuing
Make sure you start your script with wscript.exe, not cscript.exe and don't use any wscript.echo
Renaming a file
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFile "cleanup.dll" , "cleanup.exe"
about the batch cd, practice this in a console window
cd never changes to a drive, only to another map, drive: changes the active drive
d: => d:\>
c: => c:\> (so now if you are on c:\)
cd d:\test =>c:\ (changes your active map on d: to d:\test but since your c: drive is still the active drive you see nothing happening)
d: => d:\test (change drive to d:, you do see that the active map on drive d: is d:\test (at least with the default prompt)

batch file: hide console when using start /wait

#echo off
start /wait notepad.exe somefile.txt
if exist somefile.txt echo it exists.
this will display the normal console screen as well as notepad until notepad is closed. i don't want the console screen to appear; notepad must have focus; the 'if exist' line must not run until notepad is closed. the script is run from 'total commander' but running it from 'start/run' returns same results. the second line of code is irrelevant for this example.
cmd.exe /k does same thing.
internal batch commands only.
thanks!
win 7 64 bit
You can use Windows Script Host to run your batch file. For example, create the file "startmybatch.vbs" with the following content:
Set ws = WScript.CreateObject("WScript.Shell")
cmd = "c:\mypath\mybatch.bat"
ret = ws.Run(cmd, 0, True)
Set ws = Nothing
then run "startmybatch.vbs" directly, or using "wscript startmybatch.vbs". At least on XP this works (console window is not visible). I think this will work on all Windows versions >= Win98.

Resources