如果程序通过批处理脚本运行,如何启动 wav 文件

发布于 2024-12-29 06:38:21 字数 640 浏览 0 评论 0原文

当我的 VPN 客户端断开连接时,我尝试播放 wav 文件。

遵循 Matt Lacey 的建议,来自 How通过批处理脚本检查进程是否正在运行

我有以下内容:

del search.log
tasklist /FI "WINDOWTITLE eq VPN Client" /FO CSV > search.log
FOR /F %%A IN (search.log) DO IF %%~zA EQU 0 GOTO end
start alarm.wav
:end

当search.log的内容为空时,alarm.wav播放。
但我不希望它在 search.log 为空时播放。我希望它在 search.log 包含以下信息时播放:

"Image Name","PID","Session Name","Session#","Mem Usage"
"vpngui.exe","2408","Console","0","10,496 K"

任何帮助将不胜感激。

I'm trying to play a wav file when my vpn client disconnects.

Following the advice of Matt Lacey from How to check if a process is running via a batch script

I have the following:

del search.log
tasklist /FI "WINDOWTITLE eq VPN Client" /FO CSV > search.log
FOR /F %%A IN (search.log) DO IF %%~zA EQU 0 GOTO end
start alarm.wav
:end

When the contents of search.log are empty, alarm.wav plays.
But I don't want it to play when search.log is empty. I want it to play when search.log has info in it such as:

"Image Name","PID","Session Name","Session#","Mem Usage"
"vpngui.exe","2408","Console","0","10,496 K"

Any assistance would greatly be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑饮青盏花 2025-01-05 06:38:21

Matt Lacey 解决方案希望在进程未运行时执行某些操作。因此它检查输出的大小是否为 0 长度(意味着没有找到进程)。

如果进程正在运行,您想要执行某些操作,因此您的逻辑是相反的。您需要检查输出是否为 <> 0(意味着必须已找到进程)

del search.log
tasklist /FI "WINDOWTITLE eq VPN Client" /FO CSV > search.log
FOR /F %%A IN (search.log) DO IF %%~zA NEQ 0 GOTO end
start alarm.wav
:end

可以将其修改为无需输出文件即可工作。请注意,如果找到进程,TASKLIST 的输出将是多行,但您只想播放一次警报,因此需要 GOTO。

for /f %%a in ('tasklist /FI "WINDOWTITLE eq VPN Client" /FO CSV') do (
  start alarm.wav
  goto :break
)
:break

注意 - Matt Lacey 解决方案依赖于这样一个事实:如果 TASKLIST 命令未找到进程,则它不会生成任何输出。这在 XP 上运行得很好。但在 Vista 上,如果未找到匹配的进程,TASKLIST 将生成以下行 - “INFO:没有运行与指定条件匹配的任务。”

要使该进程在任何版本的 Windows 上运行,您需要按照 Andriy M 的建议做一些事情。这是一个不需要输出文件的变体

tasklist /FI "WINDOWTITLE eq VPN Client" /FO CSV | FIND /I ".exe" >nul && start alarm.wav

| 是一个管道运算符。它导致 TASKLIST 命令的输出直接通过管道输入作为 FIND 命令的输入。

(编辑 - 添加 /I 选项,以防可执行文件名使用大写)

The Matt Lacey solution wants to do something if a process is NOT running. So it checks if the size of the output is 0 length (meaning no process found)

You want to do something if a process IS running, so your logic is inverted. You need to check if the output is <> 0 (meaning a process must have been found)

del search.log
tasklist /FI "WINDOWTITLE eq VPN Client" /FO CSV > search.log
FOR /F %%A IN (search.log) DO IF %%~zA NEQ 0 GOTO end
start alarm.wav
:end

This can be modified to work without the need of an output file. Note that the output of TASKLIST will be multiple lines if the process is found, but you only want to play the alarm once, hence the GOTO is needed.

for /f %%a in ('tasklist /FI "WINDOWTITLE eq VPN Client" /FO CSV') do (
  start alarm.wav
  goto :break
)
:break

Note - The Matt Lacey solution relies on the fact that the TASKLIST command will not produce any output if it does not find the process. That works fine on XP. But on Vista TASKLIST will produce the following line if no matching process is found - "INFO: No tasks are running which match the specified criteria."

To get the process to work on any version of Windows, you need to do something along the lines of what Andriy M suggests. Here is a variation that eliminates the need for an output file

tasklist /FI "WINDOWTITLE eq VPN Client" /FO CSV | FIND /I ".exe" >nul && start alarm.wav

The | is a pipe operator. It causes the output of the TASKLIST command to be piped directly in as input to the FIND command.

(Edit - added the /I option in case the executable file name uses upper case)

┈┾☆殇 2025-01-05 06:38:21

您可以使用FIND命令测试日志文件的实际内容,然后根据测试结果执行所需的操作。操作方法如下:

del search.log
tasklist /FI "WINDOWTITLE eq VPN Client" /FO CSV > search.log
FIND ".exe" < search.log > NUL && start alarm.wav

FIND 命令将在日志文件中搜索 .exe 字符串。 (如果前面的命令确实在进程列表中找到了请求的程序,您会期望它包含 .exe ,不是吗?)此时实际的行并不重要,因此我们丢弃FIND的输出(> NUL)。重要的是是否找到该行。如果是,则将调用&&之后的命令,即start Alarm.wav。如果没有,则不会有警报。

You could test the actual contents of the log file using the FIND command, and then perform the required action based on the result of the test. Here's how:

del search.log
tasklist /FI "WINDOWTITLE eq VPN Client" /FO CSV > search.log
FIND ".exe" < search.log > NUL && start alarm.wav

The FIND command will search for the .exe string in the log file. (You would expect it, wouldn't you, to contain .exe if the previous command did find the requested program in the process list.) The actual line is not important at this moment, so we are discarding the FIND's output (> NUL). What is important is the fact whether the line is found at all. If it is, the command following &&, i.e. start alarm.wav, will be invoked. If not, there will be no alarm.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文