如何获取文件或目录的标准化日期/时间戳.在纯批处理脚本中? [英] How to get standardized date/time stamp of file or dir. in pure batch scripting?

查看:166
本文介绍了如何获取文件或目录的标准化日期/时间戳.在纯批处理脚本中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Windows命令行中是否有一种方法可以以标准化的与区域设置无关格式(例如, ISO8601 )?

Is there a way in Windows command line to retrieve the date/time stamps (modification, creation, access) of a file or directory in a standardized locale-independent format (for instance, ISO8601)?

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

I found that the output of dir uses system-dependent date/time format, with the seconds missing.
The same is true for the ~t modifier of the parameter (%~t1) and for variable expansion (%~tI).
Also the forfiles variables @fdate and @ftime depend on the system settings (although the latter returns also the seconds at least).

我了解到wmic OS GET LocalDateTime /VALUE是获取当前系统日期/时间的一种方式(输出看起来像LocalDateTime=20151021020000.000000+120,它可以由for /F捕获).
但是是否还有一条命令(行)以类似的格式返回文件或目录的日期/时间戳?

I have learned that wmic OS GET LocalDateTime /VALUE is a way to get the current system date/time (the output may look like LocalDateTime=20151021020000.000000+120, it can be captured by for /F).
But is there also a command (line) that returns a file or directory date/time stamp in a similar format?

推荐答案

Here you can find a ways to get the times of a file.

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

1)For directory you can use this WMIC query :

@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):

2) or with jscript hybrid (just like in the file version you can use also LastModified , CreationDate or LastAccessed):

@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格式化功能,它可能是最强大的选项):

3) with self-compiled jscript.net (which can be the most powerful option because of .NET formatting capabilities):

@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"));

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

EDIT Here are ready to use parametrized scripts using all the listed methods:

  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. 的dir时间戳
  1. fileModifiedTime.bat - gets last modified time of file with settings independent format.Based on robocopy
  2. fileTimes.bat - gets file time stamps with WMIC
  3. dirTimes.bat - gets directory time stamps with WMIC
  4. fileTimesJS.bat - file time stamps with jscript
  5. dirTimesJS.bat - directory time stamps with jscript
  6. fileTimesNET.bat - file time stamps with .NET
  7. dirTimesNET.bat - dir time stamps with .NET

这篇关于如何获取文件或目录的标准化日期/时间戳.在纯批处理脚本中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆