【问题标题】:How to get file modification date in 24 hour format using windows batch file如何使用 Windows 批处理文件以 24 小时格式获取文件修改日期
【发布时间】:2020-11-25 01:51:09
【问题描述】:

来自this的帖子,可以得到文件修改日期

ECHO Last-Modified Date   : %%~t? 

returns YYYY-MM-DD HH:MM <AM/PM>. 

但是,我需要它是 24 小时格式的。

怎么做?

TIA!

【问题讨论】:

  • 一种方法是在字符串包含 PM 时将 12 添加到小时
  • @Marged 仅将 12 添加到包含 PM 的字符串的小时中对于时间范围 12:00 PM12:59 PM 是错误的。这仅适用于01:00 PM11:59 PM 的时间范围。因此,如果小于12,则在使用12 进行加法之前,还需要评估小时的值。

标签: batch-file


【解决方案1】:

使用批处理文件,您可以执行以下操作:

@echo off
Title Get file modification date in 24 hour format using windows batch file
set "DesktopFolder=%userprofile%\Desktop"
set "Ext=txt"
CD /D "%DesktopFolder%"
Setlocal EnableDelayedExpansion
@FOR /F "delims=" %%A IN ('dir /B "%DesktopFolder%\*.%Ext%" 2^>nul') DO (
    SET /a "Count+=1"
    SET "File[!Count!]=%%~fA"
)

@For /L %%i in (1,1,%Count%) do (
    Call :GetLastModifiedDate "!File[%%i]!" LastModifiedDate
    echo "!File[%%i]!"  "!LastModifiedDate!"
)
Pause & EXIT
::----------------------------------------------------------------------------
:GetLastModifiedDate <File> <LastModifiedDate>
Set "vbsfile=%Temp%\%~n0.vbs"

>"%vbsfile%" (
    echo Set FSO=CreateObject("Scripting.FileSystemObject"^)
    echo WScript.Echo FSO.GetFile("%~1"^).DateLastModified
)
@for /f "delims=" %%a in ('cscript //NoLogo "%vbsfile%"') do Set "%2=%%a"
Exit /B
::----------------------------------------------------------------------------

【讨论】:

    猜你喜欢
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    相关资源
    最近更新 更多