2017-08-31 53 views
1

我遇到了我編寫的批處理文件的問題,它假設檢測進程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 

在任務管理器我有一個相匹配的WMIC條件和運行,因此python.exe過程它應該被卡在循環中,但出於某種原因,「內部其他」正在打印到控制檯,我不知道我在做什麼錯誤,它似乎像我比較兩種方式中的東西..

謝謝

編輯: 我解決了這個問題,這個問題是在第一個for循環的「delims」。一旦我刪除了它的一切工作得很好。 因此得出結論 - 這是工作 「爲」 行:

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

幾個祕訣:可能更好使用'if/i「%% j」==「%exe%」...'。 '/ i'表示不區分大小寫(以防萬一),如果任何一方都是空字符串(這裏可能不是問題),那麼在每邊放置雙引號可以保護您。如果有空位(如建議刪除的答案),請使用'echo當前進程是:%% j:'(在打印的值的任意一邊立即有冒號),以便查看是否有空閒空間(和'%EXE%'相同)。 – TripeHound

+0

謝謝你的提示 – Noam

回答

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 (
+0

謝謝你的回答,但我用不同的方式解決了這個問題,看到我更新的帖子 – Noam