如何批量创建 "InstallDate" 输出并将其与 "xxx Days Ago" 合并?
How can I create and combine "InstallDate" Output with "xxx Days Ago" in batch?
下面提到的批处理文件显示安装日期(已转换):
@echo off
for /f "delims=" %%A in ('WMIC OS GET InstallDate /format:value') do (
@for /f "tokens=2 delims==" %%B in ("%%A") do (
Call :ConvertDate %%B
)>"%temp%\%~n0.txt"
)
for /f "delims=" %%D in ('Type "%temp%\%~n0.txt"') do ( set InstallDate=%%D )
echo Install Date: %InstallDate%
pause
::**********************************************************************
Rem Function for Converting WMI Dates to a Standard Date-Time Format
:ConvertDate <Date>
(
echo WScript.echo WMIDateStringToDate("%~1"^)
echo Function WMIDateStringToDate(Mydate^)
echo WMIDateStringToDate = CDate(Mid(Mydate, 5, 2^) ^& "/" ^& _
echo Mid(Mydate, 7, 2^) ^& "/" ^& Left(Mydate, 4^) _
echo ^& " " ^& Mid (Mydate, 9, 2^) ^& ":" ^& _
echo Mid(Mydate, 11, 2^) ^& ":" ^& Mid(Mydate,13, 2^)^)
echo End Function
)>"%temp%\%~n0.vbs"
cscript /nologo "%temp%\%~n0.vbs" "%~1"
Del "%temp%\%~n0.vbs"
exit /b
输出:
Install Date: 24/05/2020 12:54:28
现在,为此,我正在尝试创建和组合 Desire Output,例如:
Install Date: 24/05/2020 12:54:28 (114 Days Ago)
我尝试了好几件事但都失败了。有没有办法批量执行此操作?
谢谢。
因为您也已经在使用 wsh for your date conversion, you may as well use it to retrieve the wmi 信息。
这是一个混合 batch-file which should do that for you, without the need to write a vbs 文件,运行,然后删除它:
<!-- :
@"%__APPDIR__%cscript.exe" //NoLogo "%~f0?.wsf"
@Pause & GoTo :EOF
-->
<Job><Script Language="VBScript">
On Error Resume Next
Set objWMIService = GetObject("winmgmts://./root/cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,48)
For Each objItem in colItems
dtmInstalled = ParseDat(objItem.InstallDate)
numInstalled = DateDiff("n",dtmInstalled,ParseDat(objItem.LocalDateTime))
numInstDays = (numInstalled \ 60) \ 24
strMessage = "Install Date: " & dtmInstalled
If numInstDays = 0 Then
strMessage = strMessage & " (Today)"
ElseIf numInstDays > 1 Then
strMessage = strMessage & " (" & numInstDays & " Days Ago)"
Else
strMessage = strMessage & " (" & numInstDays & " Day Ago)"
End If
Next
WScript.Echo strMessage
WScript.Quit
Function ParseDat(ByVal strDate)
strYear = Left(strDate,4)
strMonth = Mid(strDate,5,2)
strDay = Mid(strDate,7,2)
strDat = strDay & "/" & strMonth & "/" & strYear
strHour = Mid(strDate,9,2) - strTimeShift
strMins = Mid(strDate,11,2)
strSecs = Mid(strDate,13,2)
strTime = strHour & ":" & strMins & ":" & strSecs
ParseDat = strDat & " " & strTime
End Function
</Script></Job>
由于您没有尝试自己计算天数,因此如果您决定修改您的问题,我将不会对上述任何内容进行解释、修改或调整 post投稿.
下面提到的批处理文件显示安装日期(已转换):
@echo off
for /f "delims=" %%A in ('WMIC OS GET InstallDate /format:value') do (
@for /f "tokens=2 delims==" %%B in ("%%A") do (
Call :ConvertDate %%B
)>"%temp%\%~n0.txt"
)
for /f "delims=" %%D in ('Type "%temp%\%~n0.txt"') do ( set InstallDate=%%D )
echo Install Date: %InstallDate%
pause
::**********************************************************************
Rem Function for Converting WMI Dates to a Standard Date-Time Format
:ConvertDate <Date>
(
echo WScript.echo WMIDateStringToDate("%~1"^)
echo Function WMIDateStringToDate(Mydate^)
echo WMIDateStringToDate = CDate(Mid(Mydate, 5, 2^) ^& "/" ^& _
echo Mid(Mydate, 7, 2^) ^& "/" ^& Left(Mydate, 4^) _
echo ^& " " ^& Mid (Mydate, 9, 2^) ^& ":" ^& _
echo Mid(Mydate, 11, 2^) ^& ":" ^& Mid(Mydate,13, 2^)^)
echo End Function
)>"%temp%\%~n0.vbs"
cscript /nologo "%temp%\%~n0.vbs" "%~1"
Del "%temp%\%~n0.vbs"
exit /b
输出:
Install Date: 24/05/2020 12:54:28
现在,为此,我正在尝试创建和组合 Desire Output,例如:
Install Date: 24/05/2020 12:54:28 (114 Days Ago)
我尝试了好几件事但都失败了。有没有办法批量执行此操作? 谢谢。
因为您也已经在使用 wsh for your date conversion, you may as well use it to retrieve the wmi 信息。
这是一个混合 batch-file which should do that for you, without the need to write a vbs 文件,运行,然后删除它:
<!-- :
@"%__APPDIR__%cscript.exe" //NoLogo "%~f0?.wsf"
@Pause & GoTo :EOF
-->
<Job><Script Language="VBScript">
On Error Resume Next
Set objWMIService = GetObject("winmgmts://./root/cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,48)
For Each objItem in colItems
dtmInstalled = ParseDat(objItem.InstallDate)
numInstalled = DateDiff("n",dtmInstalled,ParseDat(objItem.LocalDateTime))
numInstDays = (numInstalled \ 60) \ 24
strMessage = "Install Date: " & dtmInstalled
If numInstDays = 0 Then
strMessage = strMessage & " (Today)"
ElseIf numInstDays > 1 Then
strMessage = strMessage & " (" & numInstDays & " Days Ago)"
Else
strMessage = strMessage & " (" & numInstDays & " Day Ago)"
End If
Next
WScript.Echo strMessage
WScript.Quit
Function ParseDat(ByVal strDate)
strYear = Left(strDate,4)
strMonth = Mid(strDate,5,2)
strDay = Mid(strDate,7,2)
strDat = strDay & "/" & strMonth & "/" & strYear
strHour = Mid(strDate,9,2) - strTimeShift
strMins = Mid(strDate,11,2)
strSecs = Mid(strDate,13,2)
strTime = strHour & ":" & strMins & ":" & strSecs
ParseDat = strDat & " " & strTime
End Function
</Script></Job>
由于您没有尝试自己计算天数,因此如果您决定修改您的问题,我将不会对上述任何内容进行解释、修改或调整 post投稿.