I don't Understand how to exist task in batch script? - windows

I have made a file named CHECKPRODUCT.CMD. but I have another file that runs at startup and it is named ACTIVATED.cmd. but I don't understand how to exists task ACTIVATED.cmd in CHECKPRODUCT.cmd. like I tried this examples :
if tasklist == "ACTIVATED.cmd" (
goto :1 ) else (
goto :2 )
but, it doesn't work!
can someone help me with that ?? I NEED help. really!
thank you so much!

You can use tasklist to check many facets of tasks running and the OLD school training was give your bat files a title for exactly this reason.
First always ensure (and I often forget) use Title in a bat or cmd that is likely to not exit quickly its useful for tasklist as here and on occasion taskkill /t or considered bad if you use /F. Try to use a unique name otherwise editing activate.cmd can show up in notepad and cmd
#echo off & title ACTIVATED
echo Running Activated
pause
You can use other status tests than "running" see tasklist /?
tasklist /FI "WindowTitle eq ACTIVATED" /FI "STATUS eq running" | Find /i "cmd"
REM bad find=2 not found=1 found at least once=0
if %errorlevel% == 0 (
echo true) else (
echo false)
pause
You may need to use ....ACTIVATED*" if you want to test for multiple entries etc.

If I understand your question correctly, then you're trying to determine if another batch file is still running from another.
You can do that by checking the command line strings of running cmd.exe processes, using the WMI command line utility.
#Echo Off
SetLocal EnableExtensions
Set "ScriptName=ACTIVATED.cmd"
%SystemRoot%\System32\wbem\WMIC.exe Process Where^
"Name='cmd.exe' And CommandLine Like '%%%ScriptName:_=[_]%%%'"^
Get ProcessId /Value 2>NUL | %SystemRoot%\System32\find.exe "=" 1>NUL^
|| GoTo :2
:1
Echo %ScriptName% is running.
Pause
GoTo :EOF
:2
Echo %ScriptName% is not running.
Pause
Note: Whilst you're free to modify the script name on line 4, please be aware that this would need additional work, should you decide to use file names containing [, ], ^, or % characters.

Related

How would I check if a named program is running in a batch script?

For some reason it says awesome even when the program is not open, and even if I put in a window name like "asdfsd" or something random. Can anyone help?
#echo off
:start
tasklist | find /I "WINDOWNAME"
if errorlevel 1 (
echo awesome
)
goto :start
At first, let me recommend not to use find just to find a certain window title in the whole output of tasklist, because the search string might occur somewhere else, like the image name, for example, which could lead to false matches.
Anyway, the tasklist command does not set the exit code (ErrorLevel) when the filter /FI does not find a match, but you could check whether the output begins with INFO:, which is the case when no match was encountered:
:start
timeout /T 1
tasklist /FI "WindowTitle eq WindowName" | findstr /B "INFO:" > nul && (echo goto :start) || (echo awesome)`.
This depends on the returned single line in case of no matches:
INFO: No tasks are running which match the specified criteria.
This text depends on the locale/region/language settings of the system. To make this even locale-independent, you could use the a trick: tasklist, with default output format (/FO TABLE), returns more than a single line when at least a match is encountered, because there is a two-line header followed by the actual matching items; if there is no match, the aforementioned line is the only one returned. So capture the output of tasklist by a for /F loop, using the option skip=1. The for /F loop will then set the exit code to 1 (not the ErrorLevel though) when it does not iterate, and to 0 when it iterates at least once. This exit code can be checked using the conditional execution operators && and ||:
:start
timeout /T 1
(for /F "skip=1" %%I in ('tasklist /FI "WindowTitle eq WindowName"') do rem/) && (echo awesome) || (goto :start)
I inserted the timeout command in order to avoid heavy CPU loads by the goto :start loop.

How to find out when a program was started and then closed via cmd batch file and then create a rem on program close

I want to keep this simple. I have a ACCESS DB batch file that I want to run from a trusted users computer, or via task scheduler. Batchrun.mdb runs its queries and then exits automatically. I'd like to know how we can tell when it actually closes -- if and when it does i'd like to add a rem line that says update complete or similar with the time. Thank you! I cannot find anything on this via google. The key is knowing that the program actually opened, and closed. I will remove the exit /b if i can get this to work correctly.
Batch file:
Start "" "E:\REDACTED\Batch Files\Batchrun.mdb"
Exit /b
For adding the REM within your code, you can do Echo Rem [%date% %time%] update complete>>"%~f0" - As posted by #LotPings - And can have the batch loop using tasklist to seach for your program. Upon terminating, it will end the loop and print the REM timestamp in your script and exit.
#ECHO OFF
#SETLOCAL EnableDelayedExpansion
Start "" "E:\REDACTED\Batch Files\Batchrun.mdb"
Echo Rem [%date% %time%] Application Opened>>"%~f0"
:ProcessLoop
tasklist /FI "IMAGENAME eq Batchrun.mdb" 2>NUL | find /I /N "Batchrun.mdb">NUL
if "%ERRORLEVEL%"=="0" (GOTO :ProcessLoop) ELSE (GOTO :Closed)
:Closed
Echo Rem [%date% %time%] Application Closed>>"%~f0"
Exit /b
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: LOG

Batch File: Getting user input during execution of other commands, then checking it later

What I want is a command (or series of commands) that works with Windows 7, 8.1, and 10. It needs to collect user input at any time during the execution of the batch file it's in, only to checked and interpreted later. I do not mind using an input text file.
I've already searched for an answer, but the closest I've come to it is <nul set /p "input=", but it requires the user to press a key and hit enter at the exact moment the command is run. Any help would be greatly appreciated.
This method utilizes GOTO to create a loop which checks the first line of input.txt every 6 seconds or so. You could replace the content of :DOSTUFF with anything you want. Let me know if you have questions.
#echo off
GOTO DOSTUFF
:CHECKINPUT
for /f %%a in (input.txt) do (
if %%a NEQ "" (
set "input=%%a"
GOTO GOTINPUT
)
exit /b
)
GOTO DOSTUFF
:GOTINPUT
echo Thanks for the input!
echo Here is what you entered:
echo %input%
GOTO ENDER
:DOSTUFF
echo I could be doing other things here, but instead I'm just waiting for input...
PING 127.0.0.1 -n 6 >nul
GOTO CHECKINPUT
:ENDER
pause
While this was running in one window, I just ran echo test>input.txt in another command prompt.
To make this more robust you might want to overwrite the file after you check it. This can easily be done with echo.>input.txt

Batch - Kill program if running, start it if it's not

I'm trying to make a toggle batch script for a process so that if it's running it gets killed and if it's not it is started. This is what I have:
tasklist /FI "IMAGENAME eq ProcessName.exe" 2>NUL | find /I /N "ProcessName.exe">NUL
set procopen = %ERRORLEVEL%
if "%procopen%"=="0" taskkill /im ProcessName.exe
if NOT "%procopen%"=="0" start "" ProcessName.exe
But everytime I run it, after the first if statement I receive the error:
"1"=="0") was unexpected at this time.
I also feel like there's a more efficient way to write this, but I'm not exactly sure how. Any help is appreciated!
More efficient would be to use conditional execution.
tasklist | find /i "application.exe" >NUL && (
taskkill /im "application.exe" /f
) || (
start "" application.exe
)
I think the reason your script is failing is because you've got spaces surrounding your equal sign in your set procopen line. You're basically setting a variable named procopenspace=spacenumeral, with the spaces included in both the variable name and the value. In the set command, spaces are treated as literal space characters, rather than as token delimiters. Now if you had done set /a, it probably would've worked (as set /a is more tolerant of spacing). Or if you had left the spaces out and set "procopen=%ERRORLEVEL%", that probably would've worked too. Here's an example cmd console session to demonstrate:
C:\Users\me>set procopen = 0
C:\Users\me>set "res=%procopen%"
C:\Users\me>set res
res=%procopen%
C:\Users\me>set "res=%procopen %"
C:\Users\me>set res
res= 0

Windows batch file : PID of last process?

I am launching a browser from batch file.
START "www.google.com"
I would like to know the PID of this browser window launched.
There can be many browser windows launched on a single machine. I need to find the PID of the process which was launched by my batch file only. I tried with WINDOWTITLE filter. But its not a good idea as titles may change in future. I am using Windows XP/7
Any help would be appreciated.
Thanks.
For what it worth (question is more than 2 years old) this code do the trick, just change variable according to default browser exe
set "browser=palemoon.exe"
tasklist /FI "imagename eq %browser%" /NH /FO csv > task-before.txt
start www.google.com
tasklist /FI "imagename eq %browser%" /NH /FO csv > task-after.txt
:: fc /L /LB1 test4-Before.txt test4-After.txt | find /I "%browser%"
for /f "delims=, tokens=2,*" %%A in ('"fc /L /LB1 task-before.txt task-after.txt | find /I "%browser%""') do set pid=%%A
SETLOCAL enabledelayedexpansion
pid=!pid:"=!
ENDLOCAL
echo pid is %pid%
This is just an idea, to get you maybe on the way
there is a command called Tasklist
there is a switch called filter /FI with lets you decide what filter parameters you want to output, f.e PID. Output this to a > 1.TXT
start your proces
recheck the watchlist and output to 2.TXT
Then you would have to get creative. COmpare 1 to 2,
maybe remove the processes in 1 from the 2.TXT
The remainig PID is what you wanted?
If you have some programming experience, you could create your own console application that accepts command-line parameters and passes them to the Win32 API CreateProcess() function. One of its output values is the spawned process ID, which your app could then return. Then just update your batch file to call your app instead of using START directly.
I'm trying to do the same thing. Though there must be some way of doing it, but all my Googling suggests not.
Check out robvanderwoude.com to see a list of 3rd party tools and examples. Also check out the full list of Sysinternal's process utilities here.
I've been looking at this for about 2 hours now and I think that there is a way to do this, but it requires some more insight on how windows handles iexplore.exe for PID...
I have a working version of a batch file I wrote that will get you what you want, BUT only if its the FIRST AND ONLY Internet Explorer Window open.
For some reason I can't get the PID to change when I open new browsers, but I can get results if there is no window open (obviously because there is no PID)
Anyhow, this is what I have... you should be able to run this on your system and it will tell you that there are no differences and it might actually produce results if your default browser is Firefox or Chrome or something... just need to make the changes to what I'm providing.
#echo off
IF EXIST c:\temp\pshell.txt del c:\temp\pshell.txt
IF EXIST C:\temp\PID1.txt del C:\temp\PID1.txt
IF EXIST C:\temp\PID2.txt del C:\temp\PID2.txt
IF EXIST C:\temp\PowerFormat.txt del C:\temp\PowerFormat.txt
powershell.exe Get-Process iexplore>C:\temp\pshell.txt
FOR /F "skip=3 tokens=7 delims= " %%1 IN ( c:\temp\pshell.txt ) DO #echo %%1>> C:\temp\PID1.txt
start "title" "www.google.com"
powershell.exe Get-Process iexplore>C:\temp\pshell.txt
FOR /F "skip=3 tokens=7 delims= " %%2 IN ( c:\temp\pshell.txt ) DO #echo %%2>> C:\temp\PID2.txt
FC /L c:\temp\pid1.txt c:\temp\pid2.txt> C:\temp\FileComparison.txt
FOR /F "tokens=7 delims=" %%3 IN (c:\temp\FileComparison.txt) DO #echo %%3>C:\temp\DiffPID.txt
FINDSTR "FC: no differences encountered" c:\temp\FileComparison.txt
IF '%ERRORLEVEL%'=='0' del C:\temp\FileComparison.txt & echo.No PID Found
IF NOT '%ERRORLEVEL%'=='0' type c:\temp\FileComparison.txt
pause
exit
Let me know if this helps...

Resources