|
HAT
版主
积分 9023
发帖 5017
注册 2007-5-31
状态 离线
|
『第
16 楼』:
Re 15楼
正确
可以把多余的代码删除了:
echo %Today%
echo %FileDay%
echo %MinusD%
echo %MinusT%
|
|
|
2008-11-23 14:14 |
|
|
win007
初级用户
积分 45
发帖 44
注册 2008-11-22
状态 离线
|
『第
17 楼』:
@echo off
set SrcFile=D:\a.txt
set SrcPath=%SrcFile:\=\\%
set Today=
set FileDay=
set MinusD=
set MinusT=
for /f "tokens=2 delims==" %%a in ('wmic path win32_operatingsystem get LocalDateTime
/value') do (
set Today=%%a
)
for /f "tokens=2 delims==" %%a in ('wmic path CIM_logicalfile where name^="%SrcPath%"
get LastModified /value') do (
set FileDay=%%a
)
set /a MinusD=%Today:~0,8%-%FileDay:~0,8%
set /a MinusT=1%Today:~8,4%-1%FileDay:~8,4%
if %MinusD% leq 0 (
if %MinusT% gtr 5 (
echo "the system has been down"
) else (
echo "the system is running"
)
) else (
echo "the system has been down"
)
pause
结果如下:
‘wmic' 不是内部命令和外部命令,也不是可以运行的程序
或批处理文件。
‘wmic' 不是内部命令和外部命令,也不是可以运行的程序
或批处理文件。
无效数字。数字常数只能是十进制<17>,十六进制<0x11>或
八进制<021>。
运算符不存在。
"the system is running"
请按任意键继续...
不知道是不是环境问题,测试不通过,事实上system已经死了
[ Last edited by win007 on 2008-11-24 at 17:09 ]
|
|
2008-11-24 17:07 |
|
|
HAT
版主
积分 9023
发帖 5017
注册 2007-5-31
状态 离线
|
『第
18 楼』:
Re 17楼
操作系统的具体版本是什么?精简版的系统?
开始,运行,cmd,wmic,结果如何?
|
|
|
2008-11-24 17:15 |
|
|
win007
初级用户
积分 45
发帖 44
注册 2008-11-22
状态 离线
|
『第
19 楼』:
操作系统是 windows2000
运行cmd -->wmic
结果如下
wmic' 不是内部命令和外部命令,也不是可以运行的程序
或批处理文件。
|
|
2008-11-24 17:21 |
|
|
HAT
版主
积分 9023
发帖 5017
注册 2007-5-31
状态 离线
|
『第
20 楼』:
Re 19楼
Win2k系统没有wmic,你可以换一种方案,通过对%date%和%time%变量进行截取。你先自己尝试一下吧,遇到具体再问。
|
|
|
2008-11-24 18:03 |
|
|
win007
初级用户
积分 45
发帖 44
注册 2008-11-22
状态 离线
|
『第
21 楼』:
斑竹能不能给个sample啊
因为学了没有几天,我对bat中变量还不会运用,确实比较急。
[ Last edited by win007 on 2008-11-24 at 20:40 ]
|
|
2008-11-24 20:33 |
|
|
HAT
版主
积分 9023
发帖 5017
注册 2007-5-31
状态 离线
|
『第
22 楼』:
分别运行下面两条命令,把结果贴出来看看:
echo %date% %time%
dir /tw
|
|
|
2008-11-24 20:57 |
|
|
win007
初级用户
积分 45
发帖 44
注册 2008-11-22
状态 离线
|
『第
23 楼』:
echo %date% %time% > 1.txt
结果如下:
星期二 2008/11/25 8:56:28.38
dir /tw > 2.txt
结果如下:
驱动器 D 中的卷是 program
卷的序列号是 68A2-D7A9
d:\Documents and Settings\cy_pjhuang\桌面 的目录
2008/11/25 08:57a <DIR> .
2008/11/25 08:57a <DIR> ..
2008/11/25 08:56a 32 1.txt
2008/11/25 08:57a 0 2.txt
2008/11/24 04:31p 7,222,784 chart information to zq.xls
2008/11/20 01:52p 30,720 chartmaintain.xls
2008/11/25 08:45a 429 CIM Desktop.LNK
2008/11/21 11:40a 66,402 CMD命令速查手册.htm
2008/11/19 05:27p 1,835 CMD查询手册.bat
2008/09/11 03:10p 537 group by.txt
2008/11/19 01:45p 27,648 issue.xls
2008/11/24 02:34p 1,488 monitor+sql1.bat
2008/11/24 04:36p 671 monitor.bat
2008/11/21 10:34a 843 send mail.txt
2008/09/08 02:28p 601 Terminal Services Client.lnk
2008/10/31 04:15p 23,040 TF PVD SPC CHART 2008.10.30.xls
2008/11/11 01:15p 18,432 TF PVD SPC CHART for IT 08.11.03.xls
2006/06/08 10:04a 656,896 TSnap.exe
2008/07/29 12:56p 519 Windows Media Player.lnk
2008/11/18 03:13p 1,674,240 分类资讯.xls
2008/08/08 01:29p 539 快捷方式 MSTSC.EXE.lnk
2008/08/08 01:35p 509 快捷方式 plsqldev.exe.lnk
2008/08/06 04:05p 405 快捷方式 ReportView.exe.lnk
2008/11/19 11:17a 405 快捷方式 SPCChartor.exe.lnk
2008/08/06 02:24p 383 快捷方式 Tmgui.exe.lnk
2008/08/21 03:35p 598 快捷方式 Welcome57.pdf.lnk
2008/11/25 08:57a 23 新建 文本文档.bat
25 个文件 9,729,979 字节
2 个目录 36,331,675,648 可用字节
|
|
2008-11-25 09:26 |
|
|
HAT
版主
积分 9023
发帖 5017
注册 2007-5-31
状态 离线
|
『第
24 楼』:
dir命令的结果,是用a和p来表示上午、下午的。请把系统时间改成下午,再运行一遍echo %date% %time%,把结果贴出来看看。
|
|
|
2008-11-25 10:02 |
|
|
win007
初级用户
积分 45
发帖 44
注册 2008-11-22
状态 离线
|
『第
25 楼』:
时间改成下午9:53后
执行
echo %date% %time%
结果如下:
星期二 2008/11/25 21:53:54.40
|
|
2008-11-25 10:18 |
|
|
HAT
版主
积分 9023
发帖 5017
注册 2007-5-31
状态 离线
|
『第
26 楼』:
本帖中我提供的方法在理论上来说都是错误的,都可能发生误判,建议不要使用。
@echo off
set SrcFile=D:\a.txt
set Today=
set FileDay=
set MinusD=
set MinusT=
set Today=%date:~4,4%%date:~9,2%%date:~12,2%%time:~15,2%%time:~18,2%
for /f "skip=5 tokens=1-2 delims= " %%a in ('dir /tw %SrcFile%') do (
set FileDay=%%a%%b
goto :compare
)
:compare
set FileDayT=%FileDay%
set FileDay=%FileDay:~0,4%%FileDay:~5,2%%FileDay:~8,2%%FileDay:~12,2%%FileDay:~15,2%
set /a MinusD=%Today:~0,8%-%FileDay:~0,8%
set /a MinusT=1%Today:~8,4%-1%FileDay:~8,4%
if "%FileDayT:~-1%" equ "p" (
set /a MinusT-=1200
)
if %MinusD% leq 0 (
if %MinusT% gtr 5 (
echo "The system is down"
) else (
echo "The system is running"
)
) else (
echo "The system is down"
)
pause
|
|
|
2008-11-25 11:17 |
|
|
win007
初级用户
积分 45
发帖 44
注册 2008-11-22
状态 离线
|
『第
27 楼』:
测试过了
只要手动 更新a.txt,显示The system is running
就算手动调整系统时间往后移时间,都显示The system is running,不会出现The sysytem is down
|
|
2008-11-25 11:32 |
|
|
HAT
版主
积分 9023
发帖 5017
注册 2007-5-31
状态 离线
|
『第
28 楼』:
使用了 Ritchie Lawrence 的日期时间转换算法
@echo off
set SrcFile=D:\a.txt
for /f "skip=5 tokens=1-2 delims= " %%a in ('dir /tw "%SrcFile%"') do (
set "FileDay=%%a %%b"
goto :compare
)
:compare
REM set "FileDay=2008/11/25 20:57"
call :DateToMins %FileDay:~0,4% %FileDay:~5,2% %FileDay:~8,2% %FileDay:~11,2% %FileDay:~14,3% mFileDay
echo FileDay: %FileDay:~0,4% %FileDay:~5,2% %FileDay:~8,2% %FileDay:~11,2% %FileDay:~14,3% mFileDay
echo FileDay2Minutes: %mFileDay%
REM set "mydate=星期二 2008/11/25 15:53:54.40"
set Today=%date:~4,4%%date:~9,2%%date:~12,2%%time:~15,2%%time:~18,2%
call :DateToMins %mydate:~4,4% %mydate:~9,2% %mydate:~12,2% %mydate:~15,2% %mydate:~18,2% mToday
echo Today: %mydate:~4,4% %mydate:~9,2% %mydate:~12,2% %mydate:~15,2% %mydate:~18,2% mToday
echo Today2Minutes: %mToday%
set /a mMinus=mToday-mFileDay
echo Minus: %mMinus%
if %mMinus% gtr 5 (
echo "The system is down"
) else (
echo "The system is running"
)
pause
goto :eof
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:DateToMins %yy% %mm% %dd% %hh% %mm% result
::
:: By: Ritchie Lawrence, updated 2003-04-03. Version 1.1
::
:: Func: Returns the number of elapsed minutes since 1970-01-01 00:00
:: for a given date. For NT4/2K/XP/2003
::
:: Args: %1 years to convert, 2 or 4 digit (by val)
:: %2 months to convert, 1/01 to 12, leading zero ok (by val)
:: %3 days to convert, 1/01 to 31, leading zero ok (by val)
:: %4 hours to convert, 1/01 to 12 for 12hr times (minutes must be
:: suffixed by 'a' or 'p', 0/00 to 23 for 24hr clock (by val)
:: %5 mins to convert, 00-59 only, suffixed by a/p if 12hr (by val)
:: %6 var to receive number of elapsed minutes (by ref)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal ENABLEEXTENSIONS
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 {%nn:~2,1%} EQU {p} if "%hh%" NEQ "12" set hh=1%hh%&set/a hh-=88
if {%nn:~2,1%} EQU {a} if "%hh%" EQU "12" set hh=00
if {%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
|
|
|
2008-11-25 15:42 |
|
|
win007
初级用户
积分 45
发帖 44
注册 2008-11-22
状态 离线
|
『第
29 楼』:
刚刚测试过
结果如下:
FileDay: 2008 11 25 04 35p mFileDay
FileDay2Minutes: 20460515
无效数字。数字常数只能是十进制(17),十六位进制(0x11)或
八进制(021)。
无效数字。数字常数只能是十进制(17),十六位进制(0x11)或
八进制(021)。
Today: ~4,4mydate:~9,2mydate:~12,2mydate:~15,2mydate:~18,2 mToday
Today2Minutes:
Minus: -20460515
"The system is running"
请按任意键继续 . . .
|
|
2008-11-25 16:59 |
|
|
HAT
版主
积分 9023
发帖 5017
注册 2007-5-31
状态 离线
|
『第
30 楼』:
下午工作比较忙,太粗心了。
@echo off
set SrcFile=D:\a.txt
for /f "skip=5 tokens=1-2 delims= " %%a in ('dir /tw "%SrcFile%"') do (
set "FileDay=%%a %%b"
goto :compare
)
:compare
REM set "FileDay=2008/11/25 20:57"
call :DateToMins %FileDay:~0,4% %FileDay:~5,2% %FileDay:~8,2% %FileDay:~11,2% %FileDay:~14,3% mFileDay
echo FileDay: %FileDay:~0,4% %FileDay:~5,2% %FileDay:~8,2% %FileDay:~11,2% %FileDay:~14,3% mFileDay
echo FileDay2Minutes: %mFileDay%
REM set "mydate=星期二 2008/11/25 15:53:54.40"
call :DateToMins %date:~4,4% %date:~9,2% %date:~12,2% %time:~15,2% %time:~18,2% mToday
echo Today: %date:~4,4% %date:~9,2% %date:~12,2% %time:~15,2% %time:~18,2% mToday
echo Today2Minutes: %mToday%
set /a mMinus=mToday-mFileDay
echo Minus: %mMinus%
if %mMinus% gtr 5 (
echo "The system is down"
) else (
echo "The system is running"
)
pause
goto :eof
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:DateToMins %yy% %mm% %dd% %hh% %mm% result
::
:: By: Ritchie Lawrence, updated 2003-04-03. Version 1.1
::
:: Func: Returns the number of elapsed minutes since 1970-01-01 00:00
:: for a given date. For NT4/2K/XP/2003
::
:: Args: %1 years to convert, 2 or 4 digit (by val)
:: %2 months to convert, 1/01 to 12, leading zero ok (by val)
:: %3 days to convert, 1/01 to 31, leading zero ok (by val)
:: %4 hours to convert, 1/01 to 12 for 12hr times (minutes must be
:: suffixed by 'a' or 'p', 0/00 to 23 for 24hr clock (by val)
:: %5 mins to convert, 00-59 only, suffixed by a/p if 12hr (by val)
:: %6 var to receive number of elapsed minutes (by ref)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal ENABLEEXTENSIONS
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 {%nn:~2,1%} EQU {p} if "%hh%" NEQ "12" set hh=1%hh%&set/a hh-=88
if {%nn:~2,1%} EQU {a} if "%hh%" EQU "12" set hh=00
if {%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
|
|
|
2008-11-25 17:30 |
|
|