I have an Access application I'm running on a workstation that is used to monitor production on the floor. The application refreshes data every minute or so, but for whatever reason it stops updating once or twice a day and sits. I can catch the error, which means I can close the application when I see the error. I have not been able to figure out why it stops updating/connecting but that's not the reason for my post.
I'd like to create a batch file I can run regularly to check if Access is open. If it is, then do nothing, if it isn't, then open this data base file. Here's the batch file right now:
#echo off
QPROCESS "MSACCESS.exe">NUL 2>NUL
IF %ERRORLEVEL% EQU 0 GOTO :FIN
"C:\users\public\myfile.accdb"
exit
:FIN
exit
This works well for the most part. If Access is running the batch file flashes and does nothing. If Access is not running, the file opens up. My problem is that if Access is not running and the file opens up, the command prompt window will hang open and sit there until the Access file is closed out. I need the batch file to finish running after it opens the file, and then close out.
What am I missing? Thanks in advance.
Compo's reply to my question worked for me.I changed the line to launch the file to include a start command for MSACCESS.exe and then it worked and closed the window after running.
#echo off
QPROCESS "MSACCESS.exe">NUL 2>NUL
IF %ERRORLEVEL% EQU 0 GOTO :FIN
start MSACCESS.exe "C:\users\public\myFile.accdb"
exit
:FIN
exit
Thank you!
Related
So I wrote a batch file to open and close an application after 5 seconds (In Windows 7). Here is the code:
if "%1" == "" start "" /min "%~dpnx0" MY_FLAG && exit
#echo off
cd "C:\Users\owner\Downloads\"
Start "" /b ThrottleStop.exe
timeout /T 5 /nobreak >nul
taskkill /IM ThrottleStop.exe /F
exit
I will now give an instance of when in works correctly and when it does not:
Test 1: Run batch file by clicking on it.
Result: Command prompt Opens, code runs, the Application open and closes after 5 seconds. This test is a success.
Test 2: Place a shortcut to the same Batch file in the startup folder to execute upon each log in.
Result: When logged in the command prompt opens and appears to start running. However application NEVER opens, the batch file simply exits. This test is a failure.
Test 3: Set Task Scheduler to execute Batch file every login. When logged in the command prompt opens and appears to start running. However application NEVER opens, the batch file simply exits. This test is a failure.
Following Test 2 and 3 I tried running it manually again and it executes correctly. So the issue is only auto running it at startup. Is it possible that it needs a delay in it due to certain processes not booting up yet (on Windows side)?
If you want ThrottleStop to exit 5 seconds after it starts, add this line to the ThrottleStop.INI configuration file.
ExitTime=5
If you want to know how to properly use the Task Scheduler, follow the ThrottleStop Task Scheduler Guide exactly. There are options in the Task Scheduler that need to be disabled.
http://forum.notebookreview.com/threads/the-throttlestop-guide.531329/#post-6865107
I want to create a batch file that can detect an executable is running or not and if not, it should start it immediately after the executable is closed.
I tried wait command but it doesn't seems to work as I wanted, like when I type:
#echo off
:Restart
start "SY" /wait "C:\Program Files (x86)\SY.exe"
goto Restart
It starts my executable program and not let it close, but when the executable is minimized and restored, then it closes very easily when close button is clicked.
Then it shows a message in the command window: ^CTerminate batch job (Y/N)?
But nothing like that happens when I try the same batch code for notepad executable:
#echo off
:Restart
start "Edit Text" /wait "%windir%\Notepad.exe"
goto Restart
The notepad window never close, even by minimizing and restoring then clicking close, it does not close.
So I want the results for my executable to be the same as the results for the notepad executable. Any help would be greatly appreciated.... Thanks in advance!!!!
you can check if your .EXE is running with takslist. If it isn't running (||) then just start it. Put a loop around and you're done:
:loop
tasklist |findstr /ibc:"SY.EXE" || "C:\Program Files (x86)\SY.exe"
timeout 1
goto :loop
I am trying to execute an application from a batch file by using START command (to hide console when executing) and I need to get errorlevel after execution. For example after System.exit(10) I'd like to restart Java application:
:while
START javaw ...
IF errorlevel 10 GOTO while
But it doesn't work because errorlevel condition is evaluated before the java process has finished.
I've also tested the next code but I get an empty text file because of the same reason:
:while
start javaw ... >exit.txt 2>&1
set /p status=<exit.txt
if "%status%"=="10" goto :while
Then, is there any way to launch a Java application, without console (/WAIT is not an option), and using a loop to restart the app when a problem occurs?
See answer on question How to call a batch file in the parent folder of current batch file? to understand the 4 different methods on running an application or batch file from within a batch file.
And see also Difference between java/javaw/javaws.
The execution of the batch file results already in opening a console window.
Therefore I suggest to use simply
:while
java.exe ...
IF errorlevel 10 GOTO while
Or use following code if you don't want to see any output by the Java application in console window of the batch file:
:while
java.exe ... 1>nul 2>nul
IF errorlevel 10 GOTO while
1>nul redirects output written to stdout to device NUL and 2>nul redirects the error messages written to stderr also to device NUL.
The only solution I can think of running a Java application without displaying a console window and additionally check successful execution of the Java application would be to write a Windows (GUI) application in C/C++/C# which does not open a window like javaw.exe and which runs javaw.exe with the appropriate parameters as process with evaluating the return code.
But with a batch file it is impossible to avoid opening a console window completely as far as I know. The console window can be opened minimized, but not hidden completely.
using this solution: Windows batch assign output of a program to a variable you can do something like this:
#echo off
:loop
application arg0 arg1 > temp.txt
set /p status=<temp.txt
if "%status%"=="10" goto :loop
echo "Done."
I'm trying to write batch script to create a folder if it does not already exist.
Following up the online examples, below is my script.
The problem is; first pause works, then probably due to syntax error the window closes even before reaches to the second pause, so I can't really tell which part of my script is wrong.
Could anyone show me how to prevent closing window so that I can see what's on the window?
#echo off
:copy theme images over
:designer
echo copying theme images over...
pause
if not exist "%K2DIR%\K2 SmartForms Runtime\Styles\Themes\Sharepoint 2013\rich_text"
(
md "%K2DIR%\K2 SmartForms Runtime\Styles\Themes\Sharepoint 2013\rich_text333"
)
pause
You could put this line at the beginning of the batch file:
if not defined in_subprocess (cmd /k set in_subprocess=y ^& %0 %*) & exit )
What this line does is, the first time you run it, it re-launches itself in a subprocess that doesn't exit after it finishes running the batch file.
You need to pass the /K switch to CMD, or just open a Command Window and run the batch from the command line.
Press start and type cmd and press enter, you will launch a command prompt.
Just drag and drop what you need to run (your python script, .exe ...) into the cmd windows, and press enter.
(You might some time to run the cmd as admin: find the cmd in the start menu, right-click on it, choose run as admin).
I recorded the screen (bandicam) for when I couldn't quite read the error message, and then I could replay it; I suppose this is mainly helpful if you already have software on your computer.
using pause at end of batch paused the cmd screen, tanks!
How to prevent batch window from closing when error occurs?
I had the problem when using robocopy. My solution was:
if not %errorlevel% lss 8 pause
For Robocopy every exit code below 8 is a success:
https://ss64.com/nt/robocopy-exit.html
With batch, if you get an error, the most you see of it is a flash of text and then the program ends. Is there anyway to have it slow down? or to have it stop before closing when it hits an error?
Thanks
If you execute your Batch file from the command-line in a MS-DOS window and an error happens, you can just review the text in the window to see what happened.
On the other hand, if you execute the Batch file via a double click in the explorer you see nothing if the Batch file have an error. Is this your case? If so, the easiest solution is to test the Batch file in a MS-DOS window until it works ok.
However, if you still need a method to stop closing the DOS window when the Batch file ends, you can do that this way:
Right click on your Batch file and select Create shorcut, a Shorcut is created.
Right click on the Shortcut and select Properties
In Target, after the "C:\Path\filename.bat" string add: & PAUSE
Select OK
This way, when you execute the Shortcut via a double click, the DOS window will execute a PAUSE after the Batch file ends for any reason.
Redirect the output with > to capture it in a file.
You might need: command > file 2>&1
try this :
if NOT ["%errorlevel%"]==["0"] (
pause
exit /b %errorlevel%
)
Run the script from a present CMD.exe and add "exit /b 1" to the scripts end of file. Remove any simple "exit".
Open a new cmd window and execute your command there. The newly opened window will not be closed when an error occurs.
start cmd /k [command]
This works for me with basic commands. Not sure if it's useful for anything more advanced.
To stop a batch script before it ends, put the pause command on a new line, which will make the script wait for user input (like an enter key) before continuing (or closing).
for a second
PING -n 2 127.0.0.1 > NUL 2>&1
or for 10secs
timeout /t 10 /nobreak
This works for me. Similar to #Sri7's answer but you need the brackets and quotes:
if NOT ERRORLEVEL 0 (
pause
)