简体   繁体   English

在Batch FOR / F命令中使用空格

[英]Using spaces in a Batch FOR /F command

I'm writing a batch file in Windows to test if some processes are running and have found the following script works as expected. 我正在Windows中编写一个批处理文件,以测试某些进程是否正在运行,并且发现以下脚本可以正常工作。

set EXE=uTorrent.exe

FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF %%x == %EXE% echo uTorrent is running...

but the following fails... 但以下失败...

set EXE=Plex Media Server.exe

FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF %%x == %EXE% echo Plex is running...

I am pretty sure this is because there are spaces in the second process name? 我很确定这是因为第二个进程名称中有空格?

Could you please explain the changes needed to fix this problem? 您能否解释解决此问题所需的更改?

When using for /f and not specifying tokens= , the default is 1 token. for /f且未指定tokens= ,默认值为1个令牌。 And with a default delimiter of space, that means it's matching just the first word of your multi word search criteria. 并且使用默认的空格分隔符,这意味着它仅与多字搜索标准中的第一个单词匹配。

I tried fixing the example by adding "tokens=*" and wrapping the later comparison with double-quotes, but that just ended up capturing the full output from tasklist, which is much more than just the name of the process. 我尝试通过添加"tokens=*"并用双引号将后面的比较包装来修正示例,但这最终只是从任务列表中捕获了完整的输出,而不仅仅是过程名称。

I didn't want to just hard code in a fix that would break for the next use case, so instead I whipped up something different that'll work for you. 我不想只是硬编码可能会在下一个用例中破坏的修补程序,所以我提出了一些对您有用的不同方法。 It loves spaces and it's dynamic so it'll work for other cases later on. 它喜欢空间并且它是动态的,因此以后可以在其他情况下使用。

Cmd: Cmd:

set "zTarget=Secure System"
tasklist /nh /fi "imagename eq %zTarget%" 2>&1 | findstr /c:"%zTarget%" >nul 2>&1 && (echo yes, {%zTarget%} is running) || (echo no, {%zTarget%} is not running)

Output: 输出:

yes, {Secure System} is running

This is not the most elegant method of solving the problem, but it shows how you can do it. 这不是解决问题的最优雅的方法,但是它说明了如何实现。 We basically determine how many spaces are in the application name and then call the appropriate FOR. 我们基本上确定应用程序名称中有多少空格,然后调用适当的FOR。

set EXE=Memory Compression
set count=0
for %%a in (%EXE%) do set /a count+=1
IF "%count%"=="1" FOR /F %%G IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF "%%G" == "%EXE%" echo app is running...
IF "%count%"=="2" FOR /F  "tokens=1,2" %%G IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF "%%G %%H" == "%EXE%" echo app is running...
IF "%count%"=="3" FOR /F  "tokens=1,2,3" %%G IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF "%%G %%H %%I" == "%EXE%" echo app is running...
IF "%count%"=="4" FOR /F  "tokens=1,2,3,4" %%G IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF "%%G %%H %%I %%J" == "%EXE%" echo app is running...

Here's my initial comment as an answer: 这是我的初步评论作为答案:

@Set "EXE=Plex Media Server.exe"
@TaskList|Find /I "%EXE%">Nul&&Echo Plex is running...

If you wanted to continue with the For loop method, I'd suggest doing it like this: 如果您想继续使用For循环方法,建议您这样做:

@Set "EXE=Plex Media Server.exe"
@For /F Delims^=^" %%A In ('TaskList /Fi "ImageName Eq %EXE%" /Fo:CSV /NH'
)Do @If /I "%%A"=="%EXE%" Echo %%~nA is running...

This could be further refined by including a Find command something along the lines of ('TaskList /Fi "ImageName Eq %EXE%" /Fo:CSV /NH^|Find /I "%EXE%"') , but I have left that out because you're using an If command against the returned output. 可以通过以下方式进一步完善Find命令('TaskList /Fi "ImageName Eq %EXE%" /Fo:CSV /NH^|Find /I "%EXE%"') ,但是我已经离开了那是因为您对返回的输出使用了If命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM