How to get 2 CMD windows to 'talk' to each other? - windows

I am probably missing the right vocabulary to talk about this problem more succinctly so excuse me if I'm a little wordy here. Under Windows 10 I have a program that runs inside a CMD command prompt It's an executable called OpenSim and it has it's own extensive command set, including 'shutdown', which initiates a graceful termination of the processes therein, closes SQL connections etc, then finally closes the CMD command window. I also have a CMD .bat file that is activated by my UPS when the power goes down that will of course open it's own window, and then does some housekeeping before closing down the hardware. One thing I want the .bat file to do is to somehow insert a 'shutdown'command into the other window's process. Is that possible? If so, how? Please assume I am a total newbie at this and you won't go far wrong. Thank you.
EDIT It looks like creating a file to flag the closedown event taking place is the only (and I guess rather primitive) way to do this. So, building on what others have said in stackoverflow, I have the following now. When I run it to test it waits - it doesn't. It runs right through to the end, running 'shutdown', even though the UPSFLAG.TXT file does not exist. What's going wrong?
echo Waiting for UPS Power Down Signal.
echo =================================
#ECHO OFF
SET LookForFile="C:\Opensim OSGrid\UPSFLAG.TXT"
:CheckForFile
IF EXIST %LookForFile% GOTO FoundIt
REM If we get here, the file is not found.
REM Wait 10 seconds and then recheck.
REM If no delay is needed, comment/remove the timeout line.
TIMEOUT /T 10 >nul
GOTO CheckForFile
:FoundIt
ECHO Found: %LookForFile%
rem Tidy up
del "C:\Opensim OSGrid\UPSFLAG.TXT"
shutdown

Adding double quote after the = will save your variable as that "C:\Opensim OSGrid\UPSFLAG.TXT" which you do not want. rather you want to store it as C:\Opensim OSGrid\UPSFLAG.TXT so move the quote to before lookforfile.
Also, you created a variable for the file, so you might as well use it in the delete.
Finally, as a safety measure, always put an exit after a goto. That will ensure the system exists should there be a problem in the script and you can make sure you do not delete files or shutdown the system when it was not planned for.
echo Waiting for UPS Power Down Signal.
echo =================================
#ECHO OFF
SET "LookForFile=C:\Opensim OSGrid\UPSFLAG.TXT"
:CheckForFile
IF EXIST "%LookForFile%" GOTO FoundIt
REM If we get here, the file is not found.
REM Wait 10 seconds and then recheck.
REM If no delay is needed, comment/remove the timeout line.
TIMEOUT /T 10 >nul
GOTO CheckForFile
exit
:FoundIt
ECHO Found: %LookForFile%
rem Tidy up
del "%LookForFile"
shutdown

Related

How to execute multiple programs in the same time with a .bat file

I made 2 bat files to start apps with examples below:
My expectation is to execute them simultaneously, meaning after double click bat file, then 3 programs will pop up.
With the 1st example, the behavior is to execute outlook first, then both Mircrosoft Edge and OneNote still not pop up, until I stop Outlook.
Example 1
#echo off
"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Outlook.lnk"
"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Edge.lnk"
"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\OneNote.lnk"
exit
With the 2nd example, both Mrcrosoft Edge and OneNote were executed simultaneously, however Outlook not until I stop OneNote.
Example 2
#echo off
"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Edge.lnk"
"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\OneNote.lnk"
"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Outlook.lnk"
exit
My questions is why it behaves like this way and how to make these 3 programs start up in the same time ?
Shown below is the Windows config:
Edition Windows 10 Enterprise
Version 21H2
Installed on ‎10/‎27/‎2021
OS build 19044.1826
Experience Windows Feature Experience Pack 120.2212.4180.0
1)Run the first program using the start command.
2)Check the task list in a loop to see if the program has appeared there.
3)Impose some time limitation to the said loop.
4)Run the next program in case of success, exit with notification otherwise.
#ECHO
OFF START program1.exe
FOR /L %%i IN (1,1,100) DO (
(TASKLIST | FIND /I "program.exe") && GOTO :startnext
:: you might add here some delaying
)
ECHO Timeout waiting for program1.exe to start
GOTO :EOF
:startnext
program2.exe
:: or START
program2.exe
Remember that the timing is not precise, especially if you are going to insert delays between the task list checks.
Normally to run tasks parallel, you should add start /b before the command to run.
The start command allows the command to be executed in another process, while /b prevents the opening of a new window.
In this specific case start /b does not work, for reasons unknown to me, but you can always use cmd /c.

Windows command line. Starting processes outside of existing queue

First please excuse my english :-)
SITUATION:
I'm using queued bat processes using code from this link
Let's call it BAT1 - this file start minimized after droping a file to be processed onto it and launching pop-up window showing the actual processing - there can be several of these running and waiting for their chance to "jump in":
title Waiting
echo %1
:lockedAppend
timeout -t 10 /nobreak > NUL
2>nul (
>>%userprofile%\queue.txt (
title Working
start /wait %userprofile%\Documents\BAT2.bat %1
(call )
)
)||goto :lockedAppend
BAT2 - contains code to process file droped on BAT1 passed by parameter. BAT2 ends with:
start /min BAT3.bat "%FileToArchive%"
exit
BAT2 is NOT waiting for BAT3 to finish before continuing or exiting in this case. BAT2 closes meaning BAT1 closes and queue should IMO be free for another BAT1 to jump in.
BUT! Frome reason that is escaping me none of waiting BAT1's runs before BAT3 finishes.
QUESTION:
Is there a way to run BAT3 being ignored by the queue code of BAT1?

Want to execute two commands in windows batch file one by one

I want to send the spool files to printer from spool folder one by one and after processing each file it will be moved to another folder or delete it.
But When I'm trying to run below batch file it directly moves spool files without sending to printer which is due to batch processing.
for %%f in (C:\WINDOWS\system32\spool\PRINTERS\*.SPL) do (
echo %%~nf
start "" E:\spool\xyz\tp.exe "C:\WINDOWS\system32\spool\PRINTERS\%%~nf.SPL" "HP Printer"
move "C:\WINDOWS\system32\spool\PRINTERS\%%~nf.SPL" d:\%%~nf.txt
)
SO need any alternative option without using windows PowerShell.
Thanks in advance.
It is not because of the batch processing. By default batch waits on the termination of a command before moving on. So leaving out the start should do.
The problem is actually the start command. By default start will execute the program as a new process and won't wait for it to finish. Use start /WAIT instead, the /WAIT option will ask it to wait. But as I said earlier, you don't even need start.
Supposing your program exits when it has completed its task, you should do:
for %%f in ("C:\WINDOWS\system32\spool\PRINTERS\*.SPL") do (
echo %%~nf
E:\spool\xyz\tp.exe "%%~ff" "HP Printer"
move "%%~ff" d:\%%~nf.txt
)
In general that should do. But if the program E:\spool\xyz\tp.exe itself starts some kind of background process and exits without waiting for it to terminate, even start /WAIT won't help. As I don't know the program you're using (E:\spool\xyz\tp.exe) I won't be able to help you in that case.
EDIT: Just a little improvement I made in the code: you don't need to specify the whole path to get the file corresponding to the loop variable, %%~ff will do it for you (see this link for others).

Windows shutdown timer combined with abort timer

Because I'm lazy I made these very simple batch files
shutdown /s /t 2500
and
shutdown /a
If you are familiar with the Windows command line the first one just sets a timer after which the computer shuts down and the second one aborts this timer.
However they are two separate files. My question is: how to combine them into one? How to make 'if' statement which checks if turn off timer is engaged and aborts it and if it isn't engaged it sets the timer? Just for extra laziness I want to make it one click start/stop timer.
Also if there is a way, can I bind a batch file to specific key combination?
Mark the fact that the computer is being shut down by creating a temporary file.
If the file exists, then abort and delete the file.
#echo off
set SDF=%TEMP%\shutdown
if exist %SDF% (echo aborting shutdown
shutdown /a
del %SDF%
) else ( echo shutdown initiated
shutdown /s /t 2500
echo XX>%SDF%
)
pause

How to lock the computer after executing an exe file using batch file programming in windows?

I have a batch file program something like.
#echo off
start Mat.exe
>>Need a code here to runand check for termination of "Mat.exe"
rundll32.exe user32.dll, LockWorkStation
>>end of program
If anyone can help me set up the program so that I can lock my computer as soon as the "Mat.exe" file is terminated. I'd really appreciate it. Thanks in advance
Simply drop the start before Mat.exe so your batch-file will wait until Mat.exe has finished.
Edit: This only works if your Mat.exe runs in a console or similar.
If dropping the start didn't work you may have to check wheather or not Mat.exe is still running. For that I found a quite useful post, which should work for you: How to wait for a process to terminate to execute another process in batch file
Edit2: Complete Code:
#echo off
:LOOP
start Mat.exe
TASKLIST 2>&1 | find "Mat.exe" > nul
IF ERRORLEVEL 1 (
rundll32.exe user32.dll, LockWorkStation
) ELSE (
SLEEP 3
GOTO LOOP
)
You can adjust the SLEEP as you like, but i would suggest use at least 1 sec, otherwise you'll have 100% usage.
Try using the Start command with the /Wait switch. Here is an example:
#echo off
REM //start Mat.exe
start /wait [path of Mat.exe]
rundll32.exe user32.dll, LockWorkStation
Remember to use the 8.3 filenames and do not include (" ") in the path string

Resources