2

我有一个批处理文件,我想在 Windows 关机时运行(通过 GPO 关机脚本执行),但我只希望它在一定数量的系统关机后运行,而不是在每次关机时运行。这是因为它的作用会导致关闭过程延迟,我想减少中断。

我唯一能想到的是构建一些逻辑来检查计算机关闭了多少次以及在超过定义的阈值后执行批处理文件,但是有人有一个如何做到这一点的例子吗?我搜索了但只找到了如何每 x 个周期或时间运行一个脚本的示例,而不是每 x 运行一次。

提前致谢!

4

2 回答 2

1

您可以在每次关闭时调用批处理文件,并在顶部包含以下内容以在未达到关闭阈值时中止。

@echo off

::Test if the shutdown threshold has been met. Exit if it hasn't
setlocal
set /a "threshold=5, cnt=1"
set shutdownCountFile="C:\SomePath\shutdownCount.txt"
if exist %shutdownCountFile% for /f "usebackq" %%A in (%shutdownCountFile%) do set /a cnt=%%A+1
if %cnt% geq %threshold% (
  2>nul del %shutdownCountFile%
  endlocal
) else (
  >%shutdownCountFile% echo %cnt%
  exit /b
)

:: Your batch process goes here

您还可以使用注册表项而不是文件来跟踪关闭次数。

于 2012-08-28T14:06:35.663 回答
1
@ECHO OFF
SET LIMIT=4
SET SAVEFILE="COUNT.TXT"
SETLOCAL ENABLEDELAYEDEXPANSION

REM Get the current count or start a new file if it does not exist.
IF EXIST %SAVEFILE% GOTO READFILE
ECHO 0 >%SAVEFILE%
:READFILE
SET /P COUNT= <%SAVEFILE%

REM Increment the save file value by one.
FOR %%B IN ( "%SAVEFILE%" ) DO (
  CALL :ADD_ONE
)
ECHO %COUNT% >%SAVEFILE%
GOTO CHECK_VALUE
:ADD_ONE
SET /A COUNT+=1
GOTO :EOF

REM Conditionally reset the counter and do something.
:CHECK_VALUE
IF %COUNT% LSS %LIMIT% EXIT /B
DEL %SAVEFILE% 2>NUL

ECHO Do your stuff here...
于 2012-08-28T15:16:26.543 回答