Wait for a command to terminate - batch-file

I'm currently working on a batch file that should download files via their URL and them run formatting script on them however I don't know how to to delay the batch-file during downloading, however since it's a direct download the window doesn't stay open.
here's where I'm at :
START "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" https://www.oui.oui.fr/oui
MOVE C:\\Users\\*\\downloads\\*.csv %~dp0
EXIT
I would like to wait for the first line to finish before continuing.
Thanks for your consideration

From cmdline (cmd.exe) run start /? and you will find some help. There is a specific line in the help file for the /wait switch which reads:
WAIT Start application and wait for it to terminate.
So simply start chrome with the /wait switch:
Start /wait "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" https://www.oui.oui.fr/oui
As per your comment, the above will not work. It is probably best to test if the file exists, chrome will have a .crdownload extension when still downloading. So let's test that the *.csv.crdownload does not exist.
start /wait "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" https://www.oui.oui.fr/oui
:hold
timeout 5
if /i not exist *.csv.crdownload (MOVE "C:\\Users\\*\\downloads\\*.csv" %~dp0) else ( goto :hold)
exit

Related

How to run .EXE from .BAT inside the folder where .EXE is locating

To recreate my problem you need to understand that I have next files in 2 folders.
K:\Script.bat
K:\Project\PortChanger.exe
K:\Project\settings.xml
I want to launch PortChanger.exe using Script.bat that contains next line:
start "K:\Project\PortChanger.exe"
This script is actually executing Program.exe, but my program throws me exception since PortChanger.exe can't find Settings.xml.
How can I launch PortChanger.exe from "K:\Project\", not from "K:\"? Now it seems like .BAT taking .EXE code and just running it where .BAT is locating.
To make it even more clear:
You could use Start with its /D option:
Start "" /D "K:\Project" "K:\Project\PortChanger.exe"
Open a Command Prompt window and enter start /? to read its usage information.
I would suggest you rather use pushd and popd
#echo off
pushd "K:\Project"
start "" PortChanger.exe
popd
pushd will change to the directory, launch the executable from it, then popd will return to the previous directory stored.

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 /?

How run batch script to get output in text file

Below executable return string values in command but when i execute below batch script it pop up different command line console and exits hence i am not getting any values in output.txt
how to capture this result ?
c:\
cd C:\Windows\System32
start usbinvoke.exe argument >c:\result\outpput.txt
pause
usbinvoke.exe argument > C:\result\output.txt
Start starts programs in unusual ways. See start /?
See Command to run a .bat file
Your other commands are unnecessary.
You right click a shortcut to cmd and tick Run As Administrator on the compatibility tab
c:\
cd C:\Windows\System32
usbinvoke.exe argument >c:\result\output.txt
pause
start does not wait unless you use /wait argument. Suggest remove start and just run the executable.
You cannot redirect streams with a process that does not wait as the no handle is attached to the process.
If you require start then use arguments /b (same window) and /w (same as /wait).

How to create a batch script that restarts a executable after it is closed?

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

Run browser with absolute local path from batch file

I want to start a browser (Chrome, in this case) and open a page that is located on my computer, then continue executing other commands. Right now I have this:
#echo off
if exist "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" (
cmd /C "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "%cd%\MyPage\index.htm"
)
:: More commands here
pause
exit
And of course, it says the syntax is incorrect. I am stuck here and I don't know how to do it. It would also be great to open the same file in the same tag if I execute the batch multiple times (instead of opening a new tab each time) but I don't know if Chrome, at least, has this command available. Yet, for now the problem is that I cannot run the browser "asynchronously" and move to the next command in the batch file.
Get rid of the cmd /C
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" -- "%cd%\MyPage\index.htm"
Chrome's open format can be found here HKEY_CLASSES_ROOT\ChromeHTML\shell\open\command
Replaced cmd /c with start ""
#echo off
if exist "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" (
start "" "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "%cd%\MyPage\index.htm"
)
:: More commands here
pause
exit

Resources