windows - 用于检查文件是否超过特定年龄的批处理脚本

标签 windows batch-file

我正在寻找答案...我已经在 Google 上搜索了几个小时,但还是卡住了!

我正在 Windows Server 2008 上编写一个批处理脚本,供 NSClient 用作 Nagios 检查的外部脚本。

我已经让脚本做一些不同的事情,我只需要它来检查文件年龄!该文件每天都不同,不过我已经补偿了那部分。基本上,我正在寻找:

IF %FILE%(插入代码以确定超过 20 分钟) (如果超过 20 分钟)ECHO BLAH BLAH BLAH 否则转到:123

希望大家能帮忙,我卡在这个了!!

谢谢

最佳答案

如果早于 20 分钟,这将返回设置为 1%age% 变量,否则设置为 0.

像这样启动它:Checkage.bat "c:\folder\filename.txt"

@echo off
:: Wmic removes regional differences
:: XP Pro can have some filename errors due to the short filename bug
:: XP Home does not have WMIC.EXE

set NumMin=20

call :CheckMins "%~f1"
echo %age%
goto :EOF

:CheckMins
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "stamp=%YYYY% %MM% %DD% %HH% %Min%"
call :DateToMinutes %stamp% NowMins
set "file=%~sf1"

:: can use CreationDate instead of lastmodified

WMIC DATAFILE WHERE name="%file:\=\\%" get lastmodified | find "." >file.tmp
for /f %%a in (file.tmp) do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "filestamp=%YYYY% %MM% %DD% %HH% %Min%"
del file.tmp 2>nul
if not defined yyyy goto :EOF

call :DateToMinutes %filestamp% FileMins

set /a MinsOld=%NowMins%-%FileMins%
if %MinsOld% gtr %NumMin% (set age=1) else (set age=0)
goto :EOF

:DateToMinutes
setlocal
set yy=%1&set mm=%2&set dd=%3&set hh=%4&set nn=%5
if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
set /a dd=100%dd%%%100,mm=100%mm%%%100
set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
if 1%hh% LSS 20 set hh=0%hh%
if /i {%nn:~2,1%} EQU {p} if "%hh%" NEQ "12" set hh=1%hh%&set/a hh-=88
if /i {%nn:~2,1%} EQU {a} if "%hh%" EQU "12" set hh=00
if /i {%nn:~2,1%} GEQ {a} set nn=%nn:~0,2%
set /a hh=100%hh%%%100,nn=100%nn%%%100,j=j*1440+hh*60+nn
endlocal&set %6=%j%&goto :EOF

关于windows - 用于检查文件是否超过特定年龄的批处理脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21743046/

相关文章:

python - 让 TortoiseHg 使用单独的 Mercurial 包

Python 2.7.7 包安装给出 Visual C++ cl.exe 错误 : command failed with exit status 2

batch-file - 在批处理文件中,如何从同一目录执行 .vbs?我如何获取参数?

windows - 在 Windows 批处理文件中访问剪贴板

windows - Virtualenv 在 Windows 上不断加载全局站点包

windows - docker :manifest for microsoft/windowsservercore:latest not found

php - 来自 PHP exec 的 taskkill

windows ->nul 批量命令之前或之后的重定向

windows - 怎么逃!在变量的值中?

windows - 从批处理文件创建空白文件