Batch File Not working correctly at startup - batch-file

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

Related

Close command prompt window after opening file

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!

Combine Windows CMD BAT files

I'm trying to build a .bat file that will run an executable Java SpringBoot web app jar file (keeping the cmd window open so that I can verify it started cleanly and close it/kill the process when I'm done), then wait 10 seconds to give the app time to start, then finally open it's URL in my web browser.
I've been able to get my intended functionality by breaking it down into two .bat files. The code I have below does what I want (except the echo message is repeated, but that's not a big deal).
I'd like to know how I can achieve the same functionality within a single .bat file.
I have launch.bat:
start wait.bat
java -jar C:\dev_tools\myapp.jar
which calls wait.bat:
echo Waiting for app to start before launching browser...
timeout 10
start http://localhost:8013/myapp/ && exit
Given the combined script is called launch.bat, put if not "%~1" == "" goto :JUMP on top, then the contents of launch.bat but with the first line changed to start launch.bat #, then place goto :EOF, then :JUMP, then the contents of wait.bat:
if not "%~1" == "" goto :JUMP
start launch.bat #
java -jar C:\dev_tools\myapp.jar
goto :EOF
:JUMP
echo Waiting for app to start before launching browser...
timeout 10
start http://localhost:8013/myapp/ && exit
When you now start launch.bat, it first checks if there is an argument, which should not be the case initially; so the first start command line is reached where the script executes itself, but with an argument (#) this time; the initially executed instance continues executing the rest until goto :EOF is reached, which terminates execution.
The recursively called instance will immediately continue execution at label :JUMP, where the code of the original wait.bat script is placed.
I think, this should work:
#echo off
start "MyApp" java.exe -jar C:\dev_tools\myapp.jar
echo Waiting for app to start before launching browser...
%SystemRoot%\System32\timeout.exe 10
start http://localhost:8013/myapp/
java.exe is started as separate process running parallel to cmd.exe instance processing this batch file.
So immediately after starting java.exe the information line is output by cmd.exe in initially opened console window.
Then timeout is executed to wait 10 seconds before finally the application is started with the HTTP URL.
Finally cmd.exe finishes processing the batch file as reading end of batch file which results in terminating the cmd.exe if started with option /C as done on double clicking on a batch file.
The Java application started as separate process keeps running independent on termination of cmd.exe processing the batch file.
I hope this is what you want.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
start /?
timeout /?

I want to launch 2 .bat files from another .bat file

bat files from another Main.bat file
Files contain something like follows and i want both to launch in 15 seconds delayed and stay untill i close each one of them with a "Ctrl+C", can someone please help me with this Use Case please.
Main.bat
echo Task-1:
call C:\Users\user\bat\My_bat1.bat
echo Task-2:
call C:\Users\user\bat\My_bat2.bat
My_bat1.bat
start /wait cmd.exe /k "cd PATH && mvn -P dev"
My_bat2.bat
start /wait cmd.exe /k "cd PATH && mvn -Dspring.profiles.active=dev,swagger,no-liquibase -Dspring.cloud.config.profile=dev -DskipTests=true"
If you are just asking how to delay your batch files by 15 seconds you can do it like this:
choice /t 15 /D y /n
If not can you please clarify your question?
EDIT: Based on your update, you want to add pause to your first child bat file, then the code above before the call command in your parent bat file.
You should really edit your question to include the sequence of events you want from the comments because it causes a lot of confusion.
I'll try to piece together what you want:
Start job 1, creating a new window
Do not wait for job 1 to finish, instead wait 15 seconds
Start job 2, creating a new window
If you press Ctrl+C in either window it should stop the job and close the window
I assume that if one job is terminated before or after the other is started, the other still needs to start or continue to work.
First of all, the program that currently runs - in your case that seems to be maven - actually receives Ctrl+C and decides how it wants to act on it. I'm not sure if it does in the way you want it to.
Main.bat
echo Task-1:
"%comspec%" /c "C:\Users\user\bat\My_bat1.bat"
timeout 15 /nobreak
echo Task-2:
"%comspec%" /c "C:\Users\user\bat\My_bat2.bat"
I added a timeout command between the launches. It waits 15 seconds.
If you removed /nobreak it would also be possible to interrupt the wait with a keystroke launching task#2 early
I find that call sometimes introduces wierd errors in such cases so I prefer "%comspec%" /c. It does the same thing except it does not receive back environment variables set in child file which is fine in your scenario.
My_bat1.bat
start "" "%comspec%" /c "cd PATH & mvn -P dev & timeout -1 /nobreak"
I removed /wait which prevented the second mvn instance from starting until the first one is finished or terminated.
I replaced /k with /c and added timeout to pause execution in stead of /k.
The timeout -1 /nobreak statement causes command interpreter to wait indefinitely without a chance to stop it except for Ctrl+C.
You can remove /nobreak to allow it to be closed with any keystroke if mvn exited normally (if that ever happens)
"%comspec%" is the same as cmd.exe but is preferred if Microsoft ever decides to change command interpreter exe name. Empty "" before is required because start interprets first double-quoted string as a window name.
I assume there is a folder named PATH inside current folder because it is not a variable. Furthermore, variable %PATH% is reserved for executable/library search path list and must not be assigned some random value or used with cd command unless you really know what you are doing. See path /?.
I also used & instead of && to prevent window from closing if mvn crashes.
My_bat2.bat can be changed similarly.

Batch file not executing directly (double click)

Just wrote a simple batch file to kill the given process.
#ECHO OFF
set /p ProcName=Enter process name:
taskkill /IM "%ProcName%" /t /f
I saved it as taskkill.bat . I double clicked this bat file , A command prompt window appeared and asked Enter process name: Whatever process name I gave every time it asked the same message and did not execute taskkill command at all.
Now in a cmd window I dragged the batch file and as usual it asked Enter process name: I gave the process name and it killed the process successfully.
What happened first time ? And why it is executing second time ?
Someone noticed that the batch file name is the same as the executable.
taskkill.bat
That's not going to work...
Did you executed the script with administrator priviliges the second
time? taskkill requires an administrator rights to be executed - else
it will print access dennied (you can set a pause at the end of the
script to see what happens).To kill process without need of special
permissions you can use tskill
Exactly ! But when I dragged the batch file in command prompt, that
command prompt was running without admin privilage ?
That's a great trick! - just tested it :-) .May it should be considered as a security hole

Start multiple console apps from a batch file

I'm trying to run some console application .exe files from a batch file in Windows.
However, when I run the following code it only starts the first of the apps:
"C:\Development\App\bin\Debug1\Application.exe"
timeout 5
"C:\Development\App\bin\Debug2\Application.exe"
timeout 5
"C:\Development\App\bin\Debug3\Application.exe"
timeout 5
"C:\Development\App\bin\Debug4\Application.exe"
timeout 5
"C:\Development\App\bin\Debug5\Application.exe"
timeout 5
(I've included the timeout to spread out the intial processing a bit)
Is there a way to get the script file to start the first application, then move on and start the others?
Ideally I would like the script file to start all applications in a subdirectory, so that if I had Debug\Applications\*.exe or similar it would start all applications of type .exe (and possibly waiting 5 seconds between each). Is this possible?
You can start applications in the background by using start:
start "C:\Development\App\bin\Debug1\Application.exe"
Use start /? from a command window to get further details.
For example,
start dir
will open a new command window and show you a directory listing, leaving it open when finsished.
The:
start cmd /c "ping 127.0.0.1 && exit"
command will open a new window, run a four-cycle ping on localhost then exit.
In both cases, the current window will await the next command immediately.
#echo off
for %%F in ("Debug\Applications\*.exe") do (
start "" "%%F"
timeout 5
)

Resources