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

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).

Related

set batch script for loop to NOT wait for previous iteration to complete

I have the following script which launches multiple sessions of RDP connection files at once (colleted from a folder which only contains rdp files).
If I launch this from cmd prompt, it launches all sessions in parallel (which is what I want)
for /r %i in (*.rdp) do (mstsc %~nxi /f)
**while **if i run this script, it just launches the first session then waits for the relative process to end before running the second connection and so on.
for /r %%i in (*.rdp) do (mstsc %%~nxi /f)
What I'm doing wrong? Shouldn't it be the default behavior of batch to run all commands in parallel?
I've checked this but it doesn't address my exact scenario and it doesn't work anyhow as expected (e.g. START myBatchScript.bat doens't change the "waiting for process" behavior)
EDIT
Added answer based on comments (Thanks to #Compo and #Modi)
The coding solution to replicate the cmd behavior as a batch script is using the "start" command (as below, thanks to #Compo)
#for /r %i in (*.rdp) do #(start "" mstsc "%~nxi" /f)
for the reason why this happens refer to #Mofi comments.

How to end only a single task using taskkill?

I have a .bat file that end another .bat file using taskkill but it ends up killing itself and the other .bat file.
So how do I make sure it kills only the oldest .bat file?
Because the 2nd bat file runs after the first
I can't use PID because the PID changes with each run!
taskkill /im cmd.exe
This kills all the cmd procceses including itself! I need it to kill only 1 of the processes. (that's the older 1)
I have ran into this problem before and the way I solved it is:
Have the first script (the file you want to close) looking to see if a temporary file has been made, and if it has, delete the file the close.
:loop
if EXIST %TEMP%\close.tmp (
del %TEMP%\close.tmp
exit
)
other code
goto :loop
And in the second script (the one closing the first script) just make it create the temporary file.
echo do you want to close SCRIPT1?
choice
if %ERRORLEVEL%==1 echo.>%TEMP%\close.tmp
And if you want to, you can make it so the first script runs more commands before it closes.

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

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

My batch file is closing a program too early

Thank you, for those whom took the time to read my question. I am a gamer and would like to execute a few things. First, I would like to Trigger a batch file when I click a program, how do you do that or is it even possible? Basically, activating a game, triggers the batch file.
NOw for the batch file problem, I want to execute Xpadder when I activate games (this is an mmo) and when I close the game I want Xpadder's process/service to close. Ultimately, it's auto trigger,activate,wait,terminate.
That's kind of the process I want it to go if all can be done.
Batch File:
#echo off
start "Blade" "C:\Users\user\Documents\Blade.xpadderprofile" Blade.xpadderprofile
ECHO Blade.xpadderprofile STARTED
start /w "C:\Program Files (x86)\game\games.exe" games.exe
ECHO games STARTED
taskkill /f /im Xpadder.exe
This actually works but the problem is there are two ".exe" files with mmo's. I'll start the game and it would close Xpadder too early because one launcher starts another launcher/client. Xpadder works for the first launcher but the first launcher closes so the game will start. I hope I am explaining myself clear enough.
Reference link: How to automatically close App_A when I close App_B using batchfile
Essentially, this is the same question I have but it's not very clear. What is the batch code to get Xpadder to stay on until the second launcher/client is closed not the first one?
P.S
The game has to open through the launcher then into the second launcher/client or it will not work.
here is the other clients name and path i think:
C:\Program Files (x86)\game\gamer\bin\gam.exe
How about the use of the PsExec, a small MS ulility? Using it, your batch should work:
cmd /c psexec -d "Blade" "C:\Users\user\Documents\Blade.xpadderprofile" Blade.xpadderprofile
start /w "C:\Program Files (x86)\game\games.exe" games.exe
taskkill /f /im Xpadder.exe
The file psexec.exe must be placed in folder enlisted in the system variable WINDIR or PATH, or, otherwise, you should call it with its full path, eg. *C:\Program Files\Others\pstools.exe".
You can add #echo off, salt, pepper or some green Tabasco if you have mon€y for this :D

Running Tasks Asynchronously In CMD

I am working on modifying the script my company uses for imaging pc's. I noticed that in our current script tasks are started like the following example:
#echo off
firststep.bat
secondstep.bat
thirdstep.bat
When running I notice that tasks happen one after another. My idea is to run asynchronously when possible. I looked up several examples of asynchronous process running for CMD and I found two ways that are supposed to run processes asynchronously. They are as follows:
#echo off
REM First Example
start firststep.bat
start secondstep.bat
start thirdstep.bat
and:
#echo off
REM Second Example
start /b firststep.bat
start /b secondstep.bat
start /b thirdstep.bat
I have read that /b signifies you want to run a binary process, but I do not understand fully what the difference between start /b and start is. Which of these is more preferable to be used in this circumstance? Would either of these effectively accomplish my goal of expediting our imaging process or should I be using a different way?
Thanks in advance for all help.
/b runs the program in the same window, without it each process would run in another window which closes when it exits.

Resources