I
I
Ivan2014-06-12 12:54:24
cmd/bat
Ivan, 2014-06-12 12:54:24

How to write the output of the Find command to a variable in a bat script?

For example, the command
Tasklist |Find /I /c "cmd.exe"
Here, the Find command searches the list of processes for the number of lines with the word "cmd.exe" and displays as a number

C:\Users\UserName\Desktop>TaskList | Find /I /c "cmd.exe"
2

So, the question is how to write this number, in this case - 2, into a variable that can be used further in the code.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
OvLab, 2014-06-13
@Bboohh

Maybe there are more beautiful solutions, but here's the first thing that came to mind - it works ...

@echo off
::Результат пишем во временный файл
TaskList | Find /I /c "cmd.exe" >tmp.txt
::читаем из файла в нашу переменую
for /F %%i in (tmp.txt) do set result=%%i
::используем ее далее
echo Found: %result%

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question