Windows Batch: Call after CMD finishes - batch-file

I have Windows 10 and inside of my batch file I do the following:
start cmd /k "Build_x1.bat"
start cmd /k "Build_x2.bat"
start cmd /k "Build_x3.bat"
...
start cmd /k "Build_xN.bat"
The reason that I have it as start cmd is because I want it to open in a new window; they all finish at different times and in their own window, they output their own warnings and errors of files they are compiling. As they finish, they produce a .adb file to my directory. In this same script, I want to xcopy the *.adb into a different directory. The problem I am running into is that I don't know how to start the xcopy only after all of the above cmd's finish in their own window. So I want:
start cmd /k "Build_x1.bat"
start cmd /k "Build_x2.bat"
start cmd /k "Build_x3.bat"
...
start cmd /k "Build_xN.bat"
xcopy /s/y *.adb "C:\*some directory*"
but xcopy only after all of the above start cmd have finished. Any thoughts on how I could achieve this? I've tried a few methods but all have failed and prematurely try to copy files without them being ready.

Start them with a title and check for their windows. Loop until they all are finished:
start "MyTitle" cmd /c "Build_x1.bat"
start "MyTitle" cmd /c "Build_x2.bat"
start "MyTitle" cmd /c "Build_x3.bat"
...
start "MyTitle" cmd /c "Build_xN.bat"
:loop
timeout 1
tasklist /fi "WindowTitle eq MyTitle" |find "cmd.exe" && goto :loop
echo all of them are finished
PS: if those batch file names are like in your example, you can start them with:
for /l %%i in (1,1,%n%) do start "MyTitle" cmd /c "Build_x%%i.bat"
NOTE: cmd /k keeps the windows open, so you will have to manually close each of them.

Starting the commands like you do initiates them in a different window and you release control of it from the parent. So few options, You could initiate them all at once using start /b which will start them all separately, but in the same window, but log their stdout and stderr to file (one per batch file) then the copy can be initiated after the batch is completed:
#echo off
set "list=Build_x1.bat Build_x2.bat Build_x3.bat Build_xN.bat
for %%I in (%list%) do start "" /b "%%I">>"%%~nI.log" >2%1
xcopy /s/y *.adb "C:\*some directory*"
if you want to see the results of each files output at the end of the run:
#echo off
set "list=Build_x1.bat Build_x2.bat Build_x3.bat Build_xN.bat
for %%I in (%list%) do (
echo **** Log for %%~I *** >>"%%~nI.log"
start "" /b "%%I.bat">>"%%~nI.log" >2%1
)
xcopy /s/y *.adb "C:\*some directory*"
type Build Build*.log
or to see them all in independent windows:
#echo off
set "list=Build_x1.bat Build_x2.bat Build_x3.bat Build_xN.bat
for %%I in (%list%) do start "" /b "%%I.bat">>"%%~nI.log" >2%1
xcopy /s/y *.adb "C:\*some directory*"
for %%I in (%list%) do start "%%~nI" cmd /k type "%%~nI.log"

Related

Why after calling one command the next one doesn't execute?

After calling script which turns off Windows UAC the next commands don't execute. What am i doing wrong?
call C:\Windows\System32\cmd.exe /k %windir%\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f
call "C:\1c\1CEnterprise 8.msi" /qr TRANSFORMS=adminstallrelogon.mst;1049.mst DESIGNERALLCLIENTS=1 THICKCLIENT=1 THINCLIENTFILE=1 THINCLIENT=1 WEBSERVEREXT=0 SERVER=0 CONFREPOSSERVER=0 CONVERTER77=0 SERVERCLIENT=0 LANGUAGES=RU,ENG,UKR
call xcopy C:\1c\ibases.v8i C:\Users\%USERNAME%\AppData\Roaming\1C\1CEStart\. /Y
call rmdir "C:\1c\" /S /Q
call del "C:\1c.zip" 2> nul
call cd "C:\Program Files (x86)\1cv8\common\"
call start 1cestart.exe
After executing first command cmd.exe just throws me back to the string "C:\Usera\Admin\Desktop" and that is all

I want to make .bat file which deletes temps (starts minimized and closes automatically)

I have come up with the following code which starts minimized, waits 5 seconds (for slow PC) deletes temp files after and should automatically close, but for some reason, automatic close is not working, .bat file stays minimized.
I tried using exit command but it has 0 effect because goto :EOF prevent it from execution, but if I will remove goto :EOF script won't delete temp files
if not DEFINED IS_MINIMIZED set IS_MINIMIZED=1 && start "" /min "%~dpnx0" %* && exit
PING localhost -n 5 >NUL
#echo off
setlocal
call :Clear_Folder %SystemRoot%\TEMP
pushd C:\Users
for /d %%k in (*) do if exist "%%k\AppData\Local\Temp" call :Clear_Folder "%%k\AppData\Local\Temp"
popd
endlocal
goto :EOF
:Clear_Folder
pushd "%~1"
for /d %%i in (*) do rd /s /q "%%i"
del /f /q *
popd
goto :EOF
exit
I'm looking forward to fix last step auto close, all other features work fine, the script starts minimized, it deletes temp files but after all of this it won't close itself and it stays minimized.
The reason your minimized script is not closing at the end is that you started the script directly, instead of as an argument to cmd.exe with it's /C option. When you run your script directly via Start, cmd.exe is run using the /K option with your batch file as its argument. When the /K option is used, as explained from running cmd /?, the window remains open upon completion of the command. To close that window you need to explicitly exit the cmd.exe instance:
Here's my take on what you intended to do:
#If Not Defined IS_MINIMIZED Set "IS_MINIMIZED=1"&Start "" /Min "%~f0"&Exit
#Echo Off
Timeout 5 /NoBreak>NUL
Call :Clear_Folder "%SystemRoot%\TEMP"
For /D %%k In ("C:\Users\*")Do If Exist "%%k\AppData\Local\Temp\" Call :Clear_Folder "%%k\AppData\Local\Temp"
Exit
:Clear_Folder
PushD "%~1" 2>NUL||Exit /B
RD /S /Q "%~1" 2>NUL
PopD
GoTo :EOF
If there's no other content beneath this, you can also remove GoTo :EOF
The goto eof statements are explicitly going to end of file, skipping anything else, including your exit statement. You therefore need to change or remove it, in this instance I added a different label to goto which has only exit in the label. The second loop does not require the goto as it will fall through the label regardless:
if not DEFINED IS_MINIMIZED set IS_MINIMIZED=1 && start "" /min "%~dpnx0" %* && exit
timeout 5>nul
#echo off
setlocal
call :Clear_Folder %SystemRoot%\TEMP
pushd C:\Users
for /d %%k in (*) do if exist "%%k\AppData\Local\Temp" call :Clear_Folder "%%k\AppData\Local\Temp"
popd
endlocal
exit
:Clear_Folder
pushd "%~1"
for /d %%i in (*) do rd /s /q "%%i"
del /f /q *
popd
To explain: goto :eof is a predefined label that will exit the current script or subroutine. It therefore will skip anything in the script and simply stops executing any further instructions.
See this link on ss64
The problem is the first line not explicitly starting cmd.exe with option /C to execute the batch file once again with a separate command process with minimized window.
#echo off
if defined IS_MINIMIZED goto ClearFolders
set "IS_MINIMIZED=1"
start "Clear Folders" /min %ComSpec% /C "%~f0" %*
goto :EOF
:ClearFolders
call :Clear_Folder "%SystemRoot%\TEMP"
if defined TEMP call :Clear_Folder "%TEMP%"
for /D %%k in (C:\Users\*) do if exist "%%k\AppData\Local\Temp" call :Clear_Folder "%%k\AppData\Local\Temp"
goto :EOF
:Clear_Folder
pushd "%~1"
if errorlevel 1 goto :EOF
rd /Q /S "%~1" 2>nul
popd
goto :EOF
See also my answer on How to delete files/subfolders in a specific directory at the command prompt in Windows? It explains why rd /Q /S "%~1" 2>nul is enough to delete all subfolders and files in directory of which path is passed with argument 1 to the subroutine Clear_Folder if that directory is really existing and pushd successfully made it the current directory for the command process processing the batch file.
See also: Where does GOTO :EOF return to?
What happens after execution of first goto :EOF depends on how this batch file was started and in which environment.
A double click on a batch file results in starting the Windows command processor cmd.exe for processing the batch file with using implicit option /C to close command process after execution of the batch file. In this case the first goto :EOF results in closing initially opened console window as the Windows command process processing the batch file initially also closes.
Opening first a command prompt window results in starting cmd.exe with using implicit option /K to keep command process running after execution of a command line like executing this batch file. In this case the console window remains open after execution of first goto :EOF as the command process keeps running for further command executions by the user.
The first goto :EOF could be replaced by command exit to always exit the command process independent on how cmd.exe initially processing the batch file was started and independent on the calling hierarchy. So the usage of exit is not advisable in case of this batch file is called from another batch file which does for example more hard disk cleanup operations.

Force batch file to wait for his own process

I would like to force command line to wait for his own process, because this process app.exe must finish, and takes some time. I use artificial timeout to prevent script from closing, because if I set the timeout wrong, then app.exe gives wrong results. I have problem with correct estimate timeout.
I read something about options /w and /k but I don't know where it insert.
Maybe wait for process would be good solution, but I don't know how can I do it.
Code is below:
(
#echo instruction1 to app.exe
#echo instruction2 to app.exe
#echo instruction3 to app.exe
#echo instruction4 to app.exe
timeout 3
) | app.exe > out.txt
This script is called from another application written in c++ but not from app.exe. I have windows 7.
The line which run script:
system("script.bat");
you may use start command to make a batch script wait for itself
#echo off
if /I "%~1" EQU "runme" goto %~1
start "" /B /WAIT cmd /c "%~s0 runme %*"
exit /B
:runme
(
#echo instruction1 to app.exe
#echo instruction2 to app.exe
#echo instruction3 to app.exe
#echo instruction4 to app.exe
) | app.exe > out.txt
timeout 3
exit /B
EDIT: may I misunderstood your question, perhaps this is what you're asking for
start "" /B /WAIT cmd /c "app.exe some_parameter_here"
start "" /B /WAIT cmd /c "app.exe some_parameter_here"
start "" /B /WAIT cmd /c "app.exe some_parameter_here"
...
or
start "" /B /WAIT cmd /c "echo some_parameter_here | app.exe"
start "" /B /WAIT cmd /c "echo some_parameter_here | app.exe"
start "" /B /WAIT cmd /c "echo some_parameter_here | app.exe"
...

Call CMD START copy - background zombie process

After running the following script, it leaves behind 4 zombie cmd processes. Any explanation for this, and how can I make the processes exit?
call cmd /c start /b copy /y "%VIP_PATH%\*.txt" "P:\"
call cmd /c start /b copy /y "%VIP_PATH%\*.doc" "P:\"
call cmd /c start /b copy /y "%VIP_PATH%\*.xls" "P:\"
call cmd /c start /b copy /y "%VIP_PATH%\*.pdf" "P:\"
The "problem" is that when you invoke start /b with an internal command as argument (copy in your case), what gets executed is
"%comspec%" /k yourCommand
That is, the command processor is executed with the instruction to keep it open.
Try with
start /b "" cmd /c copy /y "%VIP_PATH%\*.txt" "P:\"

How to run a batch file minimized

I want to run a batch file in minimized mode by another batch or vbs script. What i tried is:
batch1.bat
#echo off
start /min batch2.bat
batch2.bat
#echo off
{my program}
del /f /q batch1.bat >nul
del /f /q batch2.bat >nul
When I try this everything works perfectly the second batch runs and does it's job but at the end a minimized command prompt window stays which says the windows can not fond the batch file and a simple prompt to the path of the batch file as like command prompt shows.
If i use exit command instead of this line del /f /q batch2.bat >nul from the second batch only then it works as i wish but it is important for my program to delete batch2 after it runs.
My only target is to run batch2 in minimized mode.so is there any help?i also cannot use shortcut to batch here.
From this page:
cmd.exe /c start /min YourBatchFile.bat ^& exit
When running the command from the Task Scheduler, I had to execute cmd.exe as the program and put the rest in as arguments. Also, ^& exit did not close the resulting window for me when run from Windows 8's Task Scheduler. I had to build the exit command into my batch file for it to remove the window from the task bar when it was finished.
#echo off
if not "%1" == "min" start /MIN cmd /c %0 min & exit/b >nul 2>&1
:---rest of batch below this line:
You might try this:
#ECHO OFF &SETLOCAL
{ my program }
DEL "%~f0" /F /Q
batch2.bat:
#echo off
{myprogram}
>autoDelete.bat echo del batch1.bat batch2.bat autoDelete.bat
2>nul autoDelete.bat
guys i got my problem solved while surfing on stackoverflow questions. check out the answers of this question you will find the answer.
.vbs script won't run batch / how to run batch silently
NirCMD exec hide "Path to Batch File\Batch File.bat"
download nircmd from here
Official site: http://www.nirsoft.net/utils/nircmd2.html

Resources