在您的案例中需要一个“无限循环”和python文件的组合,我想这会使您的CPU负担过重。修改了一段代码(仅在单个文件扩展名中工作(
*.bat, *.txt
)请参阅以下内容了解更一般的内容。
@echo off
setlocal EnableExtensions
:start_python_files
start "1st" "test1.py"
start "2nd" "test2.py"
start "3rd" "test3.py"
:check_python_files
call:infinite 1st test1.py
call:infinite 2nd test2.py
call:infinite 3rd test3.py
goto:check_python_files
:infinite
tasklist /FI "WINDOWTITLE eq %1 - %2" | findstr /c:PID > nul
rem findstr /c:PID command added above to confirm that tasklist has found the process (errorlevel = 0). If not (errorlevel = 1).
if %errorlevel% EQU 1 (start "%1" "%2")好吧,这种方式可能会持续一段时间,所以如果一个文件被关闭(~2-3秒取决于你的CPU过载)。
如果不适合你,请通知我。我没有安装python,也不知道它们在打开时是如何命名的:)。
那么,现在,如你所知(亲切地说???)要求完整答案让我解释我的代码:
我启用扩展(
setlocal EnableExtensions
要改变
call
命令如下:
call命令现在接受标签作为调用的目标。语法
是:
调用:标签参数
从
call /?
命令。你应该输入一个新的命令来获取更多信息
我用指定窗口标题
start
命令,这样我的代码就可以工作了。类型
start /?
在一个新的命令窗口中。
我
呼叫
这个
infinite
发送给它的子程序参数(窗口标题和文件名)。这些可以通过
%1
(第一个论点)和
%2
(第二个论点)。
在
无限的
子程序,我搜索窗口标题(
WINDOWTITLE
(等)
eq
)格式
window title - filename
.即使它不存在
tasklist
将返回
errorlevel
价值
0
带着信息:
信息:没有运行符合指定条件的任务。
正如这里
PID
字符串不存在(如果找到它就会存在),我们将
findstr
搜索它。如果找到了,
错误级别
将
0
. 否则,它会
1
.
如果
错误级别
是
1个
,这意味着找不到该进程,这意味着该文件已关闭。所以,我们用发送的参数重新打开它(
start "window title (%1)" "filename (%2)"
)
正如我们所拥有的
呼叫
埃德
无限的
子程序结束后,我们将返回
check_python_files
子程序无限地执行上述所有操作,直到用户终止或计算机关闭。
正如稍后在聊天中讨论的,当我们标准地运行python文件时(使用
start "window title"
)窗口标题将是
python.exe
文件。我找到了解决方法:
开始
这个
cmd /c
命令。修订后的代码:
@echo off
setlocal EnableExtensions
:start_python_files
start "1st" "cmd /c test1.py"
start "2nd" "cmd /c test2.py"
start "3rd" "cmd /c test3.py"
:check_python_files
call:infinite 1st test1.py
call:infinite 2nd test2.py
call:infinite 3rd test3.py
goto:check_python_files
:infinite
tasklist /FI "WINDOWTITLE eq %1" | findstr /c:PID > nul
rem findstr /c:PID command added above to confirm that tasklist has found the process (errorlevel = 0). If not (errorlevel = 1).
if %errorlevel% EQU 1 (start "%1" "cmd /c %2")我刚加了一个
命令/C
额外(并移除
% 2
)从窗口标题,因为它不需要。
CMD/C
告诉系统运行一个新的命令,该命令将执行由字符串指定的命令,然后它将终止。
简介:
应该运行命令以获取有关它们如何工作的更多信息
:
调用/?
开始/?
goto /?
tasklist /?
findstr /?
cmd /?
我建议在一个新的命令窗口中运行上述命令。
我真的很抱歉把你弄得一团糟。总之,谢谢你提供了这么好的信息让我明白我哪里错了。
















