批处理文件脚本不起作用,因为我认为它应该

时间:2021-02-15 00:37:27

标签: batch-file cmd runtime-error

我需要一个批处理脚本的帮助,该脚本检查进程 RainbowSix_BE.exe 是否正在运行。如果是,检查是否启动了另一个进程;如果不启动它。

based on @furas code

[进展]

所以我非常接近以这种方式解决问题,但是在下一次循环运行时 start 函数启动文件后,我不知道为什么即使 Rainbow 未打开,它也会关闭wallpaper.exe

from pynput import keyboard
import threading
import datetime, time

# --- functions ---

def loading():
    while running:
        print("loading", datetime.datetime.now()) #, end='\r')
        time.sleep(1)
    
def on_press(key):
    global running  # inform function to assign (`=`) to external/global `running` instead of creating local `running`

    if key == keyboard.Key.f9:
        running = True
        # create thread with function `loading`
        t = threading.Thread(target=loading)
        # start thread
        t.start()
        
    if key == keyboard.Key.f10:
        # to stop loop in thread
        running = False

    if key == keyboard.Key.f11:
        # stop listener
        return False
        
#--- main ---

with keyboard.Listener(on_press=on_press) as listener:
    listener.join()

1 个答案:

答案 0 :(得分:0)

只要您的操作对于您的错误级别是正确的,以下内容就会更像:

@Echo Off
SetLocal EnableExtensions

:Loop
%SystemRoot%\System32\tasklist.exe /Fi "ImageName Eq RainbowSix_BE.exe"^
 | %SystemRoot%\System32\find.exe ":" 1> NUL
If ErrorLevel 1 GoTo Stop

:Start
%SystemRoot%\System32\tasklist.exe /Fi "ImageName Eq wallpaper.exe"^
 | %SystemRoot%\System32\find.exe ":" 1> NUL
If ErrorLevel 1 Start "" "F:\ull\PathTo\WallPaperPlugin.exe"
%SystemRoot%\System32\timeout.exe /T 3 /NoBreak 1> NUL
GoTo Loop

:Stop
%SystemRoot%\System32\tasklist.exe /Fi "ImageName Eq wallpaper.exe"^
 | %SystemRoot%\System32\find.exe ":" 1> NUL
If Not ErrorLevel 1 (
    %SystemRoot%\System32\taskkill.exe /F /IM wallpaper.exe 1> NUL
)
%SystemRoot%\System32\timeout.exe /T 3 /NoBreak 1> NUL
GoTo Loop

我非常怀疑 WallPaperPlugin.exe 是否在 %Path% 下列出的目录中,即使是,您也不应该假设 %Path% 没有损坏。因此,我使用了一个假路径来向您表明您应该使用完整路径。如果您只是假设它在当前目录中,那么您不应该这样做,因为您显然没有在脚本中的任何地方定义它。如果它与批处理文件位于同一目录中,则您可以改用 "%~dp0WallPaperPlugin.exe"。在运行脚本之前不要忘记更改它。