I'm trying to count the number of ruby scripts I am running in a batch script.
I can get the script to list the numbers, but how do I count them. Ideally, I would receive an alert when the number of PIDs has decreased.
Thank you!
#Echo off & SetLocal EnableDelayedExpansion
set "RUBY="
for /f "tokens=2" %%A in ('tasklist ^| findstr /i "ruby.exe" 2^>NUL') do
#Set "PID=!PID!,%%A"
if defined PID Echo cmd.exe has PID(s) %PID:~1%
echo ${#PID[#]}
pause
If you just want to know how many instances of a program is running then use the count functionality of the FIND command.
FOR /F "delims=" %%G IN ('tasklist ^|find /I /C "ruby.exe"') do set count=%%G
Related
I have a BAT script that counts the number of files in a folder and exports the results into a .txt. It works great, but I'm in a situation where I need to subtract 1 from the value it's currently counting. How could I alter my script to do that?
#echo off
FOR /D %%G in ("*") DO (
PUSHD "%%G"
FOR /F "delims=" %%H in ('dir /a-d /b * ^|find /C /V ""') DO echo %%G %%H>>"..\count.txt"
POPD
)
Your goal is to use %%H with the SET /A command to do the Arithmetic. I chose to use %%G as part of the DIR command. This way you do not need PUSHD and POPD. I also chose to use an IF command to make sure the count is not zero so that it does not substract 1.
I moved the redirection of the output at the end because this opens the file for writing once instead of every time it writes a directory to the output.
The CALL command and the double percent symbols on the variable allows us to use the variable without having to enable delayed expansion.
#echo off
(FOR /D %%G in ("*") DO (
FOR /F "delims=" %%H in ('dir /a-d /b "%%G\*" 2^>NUL ^|find /C /V ""') DO (
IF NOT "%%H"=="0" SET /A "count=%%H-1"
CALL echo %%G %%count%%
)
))>count.txt
I'm having trouble assigning a command that counts the number of times a file in a directory appears to a variable.
I found that I should do this - (^ |) but when I do it and echo the variable, nothing is displayed, and there should be a number of occurrences.
My code:
#echo off
setlocal enableextensions
for /f "tokens=*" %%i in ('dir /b C:\Users\AD10FC\IdeaProjects\collateral\db-resources\release | findstr /B "!first%!-" | find /c /v ""') do set VAR=%%i
echo %VAR%
There is no need to use findstr.exe in your code to filter the leading strings, because you can filter those directly in Dir:
#Echo Off
SetLocal EnableExtensions
Set "first=LeadingString"
For /F "EOL=? Delims=" %%G In ('"Dir /B /A:-D "%UserProfile%\IdeaProjects\collateral\db-resources\release\%first%-*" | "%SystemRoot%\System32\find.exe" /V /C """') Do Set "VAR=%%G"
Echo(%VAR%
Pause
Also for the specific task you're performing, you may find the following works better for you:
#Echo Off
SetLocal EnableExtensions
Set "first=LeadingString"
For /F %%G In ('""%SystemRoot%\System32\xcopy.exe" "%UserProfile%\IdeaProjects\collateral\db-resources\release\%first%-*" . /LQH"') Do Set "VAR=%%G"
Echo(%VAR%
Pause
I am pretty new at programming in batch files, and stack overflow users have being a lot of help. My problem is almost solved. Now there is only one thing that is missing to make my script work is.
for /f "tokens=1,* delims=: " %%i in ('type "C:\dev\1597\AssayInfo.txt" ^| findstr /i CouID') do set "number=%%j"
echo %number%
in this part of the code i need to be able to find the AssayInfo.txt without the folder 1597. In my case i will have a lot of folders with random generated numbers and all of them have a Assayinfo.txt, but if i try to run the code without the 1597 path it just say that could not find the file.
We kind of went through these already, but anyway:
To actually set it as a variable after file was found.:
#echo off
setlocal enabledelayedexpansion
for /f %%i in ('dir /s /b /a-d AssayInfo.txt') do (
for /f "tokens=2" %%a in ('type "%%~fi" ^| findstr /i "CouID"') do set "number=%%a"
echo Found number !number! in file "%%~fi"
)
Hi i'm trying to find a way to get my own PID from a bat script.
I found this:
title=mycmd
tasklist /v /fo csv | findstr /i "mycmd"
that outputs:
"cmd.exe","15084","RDP-Tcp#5","2","2,768 K","Running","MEDIASERVER\Administrator
","0:00:00","Administrator: =mycmd"
how would I get the PID number into a variable in my bat script?
any help would be appreciated.
#echo off
setlocal enableextensions disabledelayedexpansion
rem Prepare a temporary file reference where to send the wmic output
for %%t in ("%temp%\%~nx0.%random%%random%%random%%random%%random%.tmp") do > "%%~ft" (
rem Call wmic to retrieve its own parent process ID, that is, our cmd instance
wmic process where "name='wmic.exe' and commandline like '%%_%random%%random%%random%_%%'" get parentprocessid
rem Read the PID from the generated temporary file
for /f "skip=1" %%a in ('type "%%~ft"') do set "processID=%%a"
rem And remove the temporary file
) & 2>nul del /q "%%~ft"
echo %processID%
try with getcmdpid , thus you will not need to change the title:
call getCmdPID.bat
echo %errorlevel%
to do it with tasklist you'll need for loop to process the output:
title mycmd
for /f "tokens=2 delims=," %%a in (
'tasklist /v /fo csv ^| findstr /i "mycmd"'
) do (
set "mypid=%%~a"
)
echo %mypid%
check also this thread:
http://www.dostips.com/forum/viewtopic.php?t=6133
I need to check which is the PID of, for example, "notebook.exe" if this process is running in the tasklist, so I need to check this each second:
If I running this while the program is running, works:
echo off
cls
SETLOCAL EnableExtensions
SETLOCAL EnableDelayedExpansion
for /f "TOKENS=1" %%a in ('wmic PROCESS where "Name='notebook.exe'" get ProcessID ^| findstr [0-9]') do set MyPID=%%a
#echo %USERNAME%-%COMPUTERNAME%-%MyPID%
But I need check this each second. The same but into a Loop, but doesn't work (MyPID variable is empty)
echo off
cls
SETLOCAL EnableExtensions
SETLOCAL EnableDelayedExpansion
for /L %%n in (1,0,10) do (
for /f "TOKENS=1" %%a in ('wmic PROCESS where "Name='notebook.exe'" get ProcessID ^| findstr [0-9]') do set MyPID=%%a
#echo %USERNAME%-%COMPUTERNAME%-%MyPID%
)
Is it possible?
You are trying to use a variable that you have set inside a for loop. Since batch is executed line by line and code blocks (such as loops) are counted as one line, this will not work because batch will first expand %MyPID% to whatever it value was, and then execute the entire for loop.
You came close to the way to fix this. Using Enable DelayedExpansion is part of the trick. but you also need to use ! around variables inside the loop to mark them as delayed.
So in conclusion, this should work:
#echo off
cls
SETLOCAL EnableExtensions
SETLOCAL EnableDelayedExpansion
for /L %%n in (1,0,10) do (
for /f "TOKENS=1" %%a in ('wmic PROCESS where "Name='notebook.exe'" get ProcessID ^| findstr [0-9]') do set MyPID=%%a
echo %USERNAME%-%COMPUTERNAME%-!MyPID!
)
However, note that loops in batch are made much easier using labels, so this would work too, removing the need for the delayed expansion:
#echo off
cls
SETLOCAL EnableExtensions
:loop
for /f "TOKENS=1" %%a in ('wmic PROCESS where "Name='notebook.exe'" get ProcessID ^| findstr [0-9]') do set MyPID=%%a
echo %USERNAME%-%COMPUTERNAME%-%MyPID%
timeout /t 1 /nobreak >nul
goto :loop
the timeout /t 1 also makes sure it waits 1 second before going back to :loop
This is better done in PowerShell:
while ($true) {
Start-Sleep -Seconds 1
"{0}-{1}-{2}" -f $env:UserName,$env.ComputerName,(Get-Process -Name "Notebook.exe").id
}