windows - 如何获取文件或目录的标准化日期/时间戳。在纯批处理脚本中?

标签 windows datetime batch-file cmd

在 Windows 命令行中是否有一种方法可以以标准化的区域设置无关格式(例如,ISO8601)检索文件或目录的日期/时间戳(修改、创建、访问) ?

我发现 dir 的输出使用系统相关的日期/时间格式,缺少秒数。
参数的~t修饰符(%~t1)和for变量扩展(%~tI).
forfiles 变量 @fdate@ftime 也取决于系统设置(尽管后者至少也返回秒数)。

我了解到 wmic OS GET LocalDateTime/VALUE 是获取当前系统日期/时间的一种方法(输出可能类似于 LocalDateTime=20151021020000.000000 +120,可以通过for/F获取)。
但是是否还有一个命令(行)以类似的格式返回文件或目录日期/时间戳?

最佳答案

Here您可以找到一种获取文件时间的方法。

1) 对于目录,您可以使用此WMIC 查询:

@echo off
set "directory=."

for /f "delims=" %%# in ("%directory%") do set "directory=%%#"

set "directory=%temp%"
for /f "usebackq delims=" %%# in (`"WMIC path Win32_Directory WHERE name='%directory:\=\\%' get creationdate,LastAccessed,LastModified /format:value"`) do (
  for /f %%@ in ("%%#") do echo %%@
)

2) 或使用 jscript hybrid(就像在文件版本中一样,您也可以使用 LastModifiedCreationDateLastAccessed):

@if (@X)==(@Y) @end /****** jscript comment ******
@echo off

set "directory=."
for %%# in (%directory%) do set "directory=%%~f#"
if exist "%directory%\" (

 cscript //E:JScript //nologo "%~f0" "%directory%"
)
exit /b %errorlevel%

****** end of jscript comment ******/

var file_loc = WScript.Arguments.Item(0);
var fso = new ActiveXObject("Scripting.FileSystemObject");
var the_file=fso.GetFolder(file_loc);
var the_date= new Date(the_file.DateLastModified);
WScript.Echo(the_date.getFullYear());
WScript.Echo(the_date.getMonth());
WScript.Echo(the_date.getUTCDate());

3) 使用自编译的 jscript.net(由于 .NET 格式化功能,它可能是最强大的选项):

@if (@X)==(@Y) @end /****** start of jscript comment ******

@echo off
::::::::::::::::::::::::::::::::::::
:::       compile the script    ::::
::::::::::::::::::::::::::::::::::::
setlocal
if exist "%~n0.exe" goto :skip_compilation

set "frm=%SystemRoot%\Microsoft.NET\Framework\"
:: searching the latest installed .net framework
for /f "tokens=* delims=" %%v in ('dir /b /s /a:d /o:-n "%SystemRoot%\Microsoft.NET\Framework\v*"') do (
    if exist "%%v\jsc.exe" (
        rem :: the javascript.net compiler
        set "jsc=%%~dpsnfxv\jsc.exe"
        goto :break_loop
    )
)
echo jsc.exe not found && exit /b 0
:break_loop


call %jsc% /nologo /out:"%~n0.exe" "%~dpsfnx0"
::::::::::::::::::::::::::::::::::::
:::       end of compilation    ::::
::::::::::::::::::::::::::::::::::::
:skip_compilation



set "directory=."
for %%# in (%directory%) do set "directory=%%~f#"


"%~n0.exe" "%directory%"


exit /b 0


****** end of jscript comment ******/
import System;
import System.IO;

var arguments:String[] = Environment.GetCommandLineArgs();

var the_dir=arguments[1];

var last_modified=Directory.GetLastWriteTime(the_dir);
Console.WriteLine("modified time "+last_modified.ToString("yyyy-MM-dd"));

var created=Directory.GetCreationTime(the_dir);
Console.WriteLine("created time "+created.ToString("yyyy-MM-dd"));

var accessed=Directory.GetLastAccessTime(the_dir);
Console.WriteLine("accessed time "+accessed.ToString("yyyy-MM-dd"));

编辑 这里准备好使用所有列出的方法使用参数化脚本:

  1. fileModifiedTime.bat - 获取具有设置独立格式的文件的最后修改时间。基于robocopy
  2. fileTimes.bat - 使用 WMIC
  3. 获取文件时间戳
  4. dirTimes.bat - 使用 WMIC
  5. 获取目录时间戳
  6. fileTimesJS.bat - 使用 jscript
  7. 文件时间戳
  8. dirTimesJS.bat - 使用 jscript
  9. 的目录时间戳
  10. fileTimesNET.bat - 使用 .NET
  11. 的文件时间戳
  12. dirTimesNET.bat - 使用 .NET
  13. 的目录时间戳

关于windows - 如何获取文件或目录的标准化日期/时间戳。在纯批处理脚本中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33248149/

相关文章:

java - Java 中的日期不正确

windows - 从批处理文件运行 regsvr32 不起作用

MySQL - 查询日期上方和下方的记录

c# - 使用给定的 DateTime 对象获取一个月的第一天和最后一天

php - 我如何返回和处理从 PHP 运行 .bat 文件的结果

windows - 针对变量的管道添加 "if arguement"

c++ - 试图在windows环境下禁用一个设备

c# - 通过命名管道在Windows上访问Docker API

windows - 如何在 Windows 中子类化一个窗口? (使用围棋)

java - Window注册表创建 key 在java中不起作用