【问题标题】:Batch script to calculate remaining days计算剩余天数的批处理脚本
【发布时间】:2022-01-09 22:39:52
【问题描述】:

在 Windows 批处理脚本中,

我想提取“直到:”之后的日期,并用当前日期计算证书到期前还剩多少天。

Serial number: 1300000119b2de558ca060bb5e000000000119
Valid from: Mon Jun 14 20:17:27 EET 2021 until: Thu Jun 08 14:36:44 EET 2023
Certificate fingerprints:

【问题讨论】:

    标签: batch-file


    【解决方案1】:

    花了几个小时在互联网上搜索后,我完成了脚本。我测试了很多场景,它看起来不错,对我来说已经足够了。谢谢。

    @ECHO OFF
    
    for /F "delims=" %%a in ('findstr "until:" %1') do set var=%%a
    set "var2=%var:*Until: =%"
    set "certyear=%var2:*EET =%"
    set "certstringmonth=%var2:~4,3%"
    for %%G in ("Jan 1" "Feb 2" "Mar 3" "Apr 4" "May 5" "Jun 6" "Jul 7" "Aug 8" "Sep 9" "Oct 10" "Nov 11" "Dec 12") do for /F "tokens=1,2" %%H in (%%G) do if %certstringmonth% == %%H set "certmonth=%%I" & goto HaveMonth
    :HaveMonth
    set "certday=%var2:~8,2%"
    for /F "tokens=* delims=0" %%I in ("%certday%") do set "certday=%%I"
    for /F "skip=1 delims=" %%F in ('
        wmic PATH Win32_LocalTime GET Day^,Month^,Year /FORMAT:TABLE
    ') do (
        for /F "tokens=1-3" %%L in ("%%F") do (
            set CurrDay=%%L
            set CurrMonth=%%M
            set CurrYear=%%N
        )
    )
    set CurrDay=%CurrDay:~-2%
    set CurrMonth=%CurrMonth:~-2%
    
    IF %certyear% GTR %CurrYear% (call:c1 "%certyear%" "%CurrYear%") ELSE (IF %certyear% LSS %CurrYear% (call:c0) ELSE (IF %certyear% == %CurrYear% ( IF %certmonth% GTR %Currmonth% (call:c4 "%certday%" "%certmonth%" "%certyear%" "%Currmonth%") ELSE ( IF %certmonth% LSS %Currmonth% (call:c0) ELSE ( IF %certmonth% == %Currmonth% ( IF %certday% GTR %Currday% (call:c5 "%certday%" "%certmonth%" "%certyear%" "%Currday%") ELSE (IF %certday% LEQ %Currday% (call:c0))))))))
    
    :c0
        ECHO 0 days remaining... It is already expired %certday%/%certmonth%/%certyear%
        EXIT
    
    :c1
        SET /A "difyear=%~1 - %~2"
        IF "%difyear%" GTR "1" (call:c2 "%difyear%" "%certday%" "%certmonth%" "%certyear%") ELSE (call:c3 "%certday%" "%certmonth%" "%certyear%" "%Currmonth%") 
        EXIT
    
    :c2
        SET /A "days = %~1 * 365"
        ECHO %days% days remaining until %~2/%~3/%~4
        EXIT
    
    :c3
        SET /A "remmonth = 12 - %~4"
        SET /A "difmonth = %remmonth% + %~2"
        SET /A "days = %difmonth% * 30"
        ECHO %days% days remaining until %~1/%~2/%~3
        EXIT
    
    :c4
        SET /A "dif2month = %~3 - %~4"
        SET /A "days = %dif2month% * 30"
        ECHO %days% days remaining until %~1/%~2/%~3
        EXIT
    
    :c5
        SET /A "days = %~1 - %~4"
        ECHO %days% days remaining until %~1/%~2/%~3
        EXIT
    

    【讨论】:

      猜你喜欢
      • 2014-06-11
      • 2013-06-26
      • 2010-11-13
      • 1970-01-01
      • 2021-06-11
      • 1970-01-01
      • 2021-08-12
      • 1970-01-01
      相关资源
      最近更新 更多