批处理文件不会输入if语句

时间:2017-08-31 12:24:25

标签: batch-file wmic

我遇到了我编写的批处理文件的问题,它假设检测进程A何时没有运行然后终止进程B.我检查进程A是否存在wmic(并且循环覆盖它,而这是这个案例)当我发现它没有运行时我只是在另一个进程上使用taskkill。问题是由于某些原因它没有输入if语句,尽管值似乎相等。

SETLOCAL EnableExtensions
set EXE=python.exe

    :RUNNERON
    FOR /F "skip=1 delims=" %%x IN ('wmic process where "CommandLine Like '%%some text to match%%' and Caption='python.exe'" get caption') DO for /F "delims=" %%j in ("%%x") do (

        echo Current process name: %%j   :: This prints "python.exe"
        echo Required process is: %EXE%  :: This prints "python.exe"
        IF %%j == %EXE% (
            echo inside if
            goto RUNNERON
        ) ELSE (
            echo inside else
        )
    )
    echo Runner is done - killing watchdog process
    taskkill /F /im "pythonw.exe"

    pause

在任务管理器中我有一个python.exe进程,它匹配wmic条件并运行所以它应该卡在循环中但由于某种原因,“内部其他”被打印到控制台,我不知道是什么我做错了,看起来像我比较两者的方式......

谢谢

编辑: 我解决了它,问题是在第一个for循环中的“delims”..一旦我删除它一切正常。 总而言之 - 这是“for”行:

FOR /F "skip=1" %%x IN ('wmic process where "CommandLine Like '%%forking import main%%' and Caption='python.exe'" get caption') DO for /F "delims=" %%j in ("%%x") do (

1 个答案:

答案 0 :(得分:1)

你的剧本中最重要的部分:

FOR /F "skip=1 delims=" %%x IN ('wmic process where "CommandLine Like '%%some text to match%%' and Caption='python.exe'" get caption') DO for /F "delims=" %%j in ("%%x") do (

应为:

For /F "Skip=1 Delims=" %%x In ('"WMIC Process Where (Caption='%EXE%' And CommandLine Like '%%some text to match%%') Get Caption"') Do For /F "Delims=" %%j IN ("%%x") Do (