Search code examples
batch-filetimewmicdays

How can I create and combine "InstallDate" Output with "xxx Days Ago" in batch?


Below mentioned batch file displays InstallDate (Converted):

@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

Output:

Install Date: 24/05/2020 12:54:28

Now, For this I am trying to create and combine Desire Output like:

Install Date: 24/05/2020 12:54:28 (114 Days Ago)

I have tried several things but failed. is there any way to do this in batch? Thanks.


Solution

  • As you're already using for your date conversion, you may as well use it to retrieve the information too.

    Here's a single hybrid which should do that for you, without the need to write a file, run it, then delete it:

    <!-- :
    @"%__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>
    

    As you've made no attempt at calculating the number of days yourself, I will not be explaining any of the above, modifying it, or adjusting it should you decide to modify your question post submission.