BATCH —将命令输出存储到变量 [英] BATCH — Store command output to variable

查看:483
本文介绍了BATCH —将命令输出存储到变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个仅在运行少于2次的cmd.exe时才执行操作的bat脚本.我设法找到了一种将出现次数存储在临时文件中的解决方案,但是我发现它很不雅致.所以我的问题是,如何在没有临时文件的情况下执行以下操作,而是通过将@TASKLIST | FIND /I /C "%_process%"给出的出现次数存储在变量中.我看到可能有一个带for循环的解决方案,但我无法使其正常工作,无论如何,我真的更希望基于SET的解决方案(如果可能).

I would like to make a bat script that performs an action only if fewer than 2 occurrences of cmd.exe are running. I managed to find a solution that stores the occurrence count in a temporary file, but I find it very inelegant. So my question is how to do the same as below without a temporary file, but rather by storing the occurence count given by @TASKLIST | FIND /I /C "%_process%" in a variable. I saw that there may be a solution with a for loop, but I couldn’t get it to work and anyway I would really prefer a solution based on SET (if this is possible).

@SET _process=cmd.exe
@SET _temp_file=tempfiletodelete.txt
@TASKLIST | FIND /I /C "%_process%" > %_temp_file%
@SET /p _count=<%_temp_file%
@DEL /f %_temp_file%
@IF %_count% LSS 2 (
    @ECHO action 1
) ELSE (
    @ECHO action 2
)

编辑:该问题类似于保存从FIND命令输出到变量,但是我无法将解决方案应用于我的问题,我想知道没有for循环的解决方案是否可能.

This question is similar to Save output from FIND command to variable, but I wasn’t able to apply the solution to my problem and I wanted to know if a solution without a for loop is possible.

推荐答案

带有选项/F的命令 FOR 和指定为 set 的命令或命令行(字符串'附加在圆括号内)可用于处理命令或一行上有多个命令的命令行的输出.

The command FOR with option /F and the command or command line specified as set (string inside parentheses) additionally enclosed by ' can be used for processing the output of a command or a command line with multiple commands on one line.

您可以使用此批处理文件:

You can use this batch file:

@ECHO OFF
SET "_process=cmd.exe"
FOR /F %%I IN ('%SystemRoot%\System32\tasklist.exe /FI "IMAGENAME eq %_process%" ^| %SystemRoot%\System32\find.exe /I /C "%_process%"') DO SET "_count=%%I"
IF /I "%_process%" == "cmd.exe" SET /A _count-=1
IF %_count% LSS 2 (
    ECHO action 1
) ELSE (
    ECHO action 2
)

命令 FOR 在后台运行,没有可见的控制台窗口cmd.exe /C,该命令行带有命令行:

The command FOR runs in background without a visible console window cmd.exe /C with the command line:

C:\Windows\System32\tasklist.exe /FI "IMAGENAME eq cmd.exe" | C:\Windows\System32\find.exe /I /C "cmd.exe"

TASKLIST 选项/FI "IMAGENAME eq cmd.exe"已将 TASKLIST 的输出过滤到具有映像名称(=可执行文件的名称)cmd.exe的进程.这样可以加快进一步处理的速度,并避免与问题命令行一样将文件名为totalcmd.exe的正在运行的进程计为cmd.exe.

TASKLIST option /FI "IMAGENAME eq cmd.exe" filters the output of TASKLIST already to processes with image name (= name of executable) cmd.exe. This makes further processing faster and avoids that a running process with file name totalcmd.exe is counted as cmd.exe as the command line in question does.

为处理 STDOUT 而编写的 TASKLIST 的输出被重定向为处理命令 FIND STDIN .行并计算行中任何位置包含cmd.exe的行. FIND 最终输出包含搜索到的字符串的行数,以处理在后台运行的命令进程的 STDOUT .

The output of TASKLIST written to handle STDOUT is redirected to handle STDIN of command FIND which processes the lines and counts the lines containing cmd.exe anywhere in line. FIND outputs finally the number of lines containing searched string to handle STDOUT of command process running in background.

当Windows命令解释器在执行命令 FOR之前处理此命令行时,重定向操作符|必须在 FOR 命令行上使用脱字符号^进行转义,以将其解释为文字字符. ,它将在后台启动的单独命令过程中执行嵌入式命令行.

The redirection operator | must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded command line in a separate command process started in background.

FOR 捕获为处理已执行命令过程的 STDOUT 而编写的所有内容,并处理每一行.在这种情况下, FOR 仅捕获一行,其中仅包含一个数字.因此,不需要其他选项即可使用命令 SET FOR 分配给循环变量I的编号分配给环境变量_count.

FOR with option /F captures everything written to handle STDOUT of executed command process and processes each line. In this case just one line is captured by FOR containing just a number. Therefore no additional options are needed to assign the number assigned to loop variable I by FOR to environment variable _count using command SET.

此批处理文件的目标是计算已经在运行的cmd.exe个进程的数量.由于 TASKLIST FIND 的命令行是由 FOR 在后台使用另一个cmd.exe进程执行的,因此必须减去使用以SET /A _count-=1评估的算术表达式加1即可得到正确的结果.仅为了正确计数cmd.exe进程的数量,才需要将此减1.不需要任何其他过程.

The goal of this batch file is counting the number of cmd.exe processes already running. As the command line with TASKLIST and FIND is executed by FOR in background with using one more cmd.exe process, it is necessary to subtract the count by one to get the correct result using an arithmetic expression evaluated with SET /A _count-=1. This decrement by one is needed only for counting right the number of cmd.exe processes. It is not necessary for any other process.

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • find /?
  • for /?
  • if /?
  • set /?
  • tasklist /?
  • echo /?
  • find /?
  • for /?
  • if /?
  • set /?
  • tasklist /?

这篇关于BATCH —将命令输出存储到变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆