当"空白"是关闭的,做这个"这个"

时间:2014-05-06 00:22:14

标签: batch-file

我对编码很新,并且想知道是否有一个命令可以告诉文件在文件关闭时执行某些操作。如果你没有在我给你的代码中实现新代码,我会更喜欢它,因为我认为通过经验学习会更好。告诉我这些命令是完美的。所以例如;这是从网上复制的“矩阵代码”:

@echo off
color 02
:start
echo %random% %random% %random% %random% %random% %random% %random% %random% %random% %random%
goto start

什么是命令,它会告诉它在程序关闭后执行操作,比如重新打开。

2 个答案:

答案 0 :(得分:1)

代码:

start /w (name of file)

打开文件,然后在继续之前等待它关闭。
如果打开的文件被调用" matrix.bat"例如,然后你使用

@echo off
start /w matrix.bat
start matrix.bat

这将启动matrix.bat,它将执行矩阵,然后当它关闭时,它会重新启动matrix.bat。
此代码需要同时打开2个批处理窗口。

注意:如果您使用的是Windows XP或更低版本,则需要/ wait而不是/ w。

答案 1 :(得分:0)

没有任何旨在实现此结果的本机命令。 但是,鉴于批处理程序可用于创建和启动其他脚本语言,您可以选择使批处理成为Vbs进程监视器,以查看cmd.exe何时关闭,然后启动您选择的另一批处理(或重新启动同一批次)。为了使此操作顺利进行,请确保已采取适当的步骤来结束vbs脚本,以避免无休止的循环。

::: Place this code at the start of any Batch, and it will make a vbs that continuously reopens the target .Bat script when
::: there's no CMD windows open.
::: ECHO WshShell.run "%~dpnx0", 0, true REM Change to "ECHO WshShell.run "%~dpnx0", 0, true" to make your Batch Invisible.
::: To change the .bat program run when your batch is closed, replace "%~dpnx0" with the full path of the other program to run.

(
ECHO Set objWMIService = GetObject ("winmgmts:"^)
ECHO Set proc = objWMIService.ExecQuery("select * from Win32_Process Where Name='cmd.exe'"^)
ECHO DO while proc.count ^> 0
ECHO Set proc = objWMIService.ExecQuery("select * from Win32_Process Where Name='cmd.exe'"^)
ECHO if proc.count ^< 1 then exit do
ECHO wscript.sleep 1500
ECHO loop
ECHO Set WshShell=createobject("wscript.shell"^)
ECHO WshShell.run "%~dpnx0",1, true
)>"%TEMP%\ReAnimator.vbs"

START "" "%TEMP%\ReAnimator.vbs"

:main & REM your batch script below


:EOF & REM Cleanup to cancel the vbs script when script is exited properly
    taskkill /pid WScript.exe /f /t >nul
    Timeout 1 >nul
    Del /F "%TEMP%\ReAnimator.vbs"
    Exit /B
  • vbs脚本调用WMIService来获取cmd.exe上的进程计数,并且只要计数为GEQ 1,就将自身保持在循环中
  • 在循环内使用睡眠来分隔对WMI服务的调用,因为这样做非常耗费资源
  • 一旦满足条件即可中断循环,它将在用于创建脚本的路径处生成运行批处理文件的cmd.exe的shell实例。
相关问题