如何获取文件或目录的标准化日期/时间戳.在纯批处理脚本中? [英] How to get standardized date/time stamp of file or dir. in pure batch scripting?
问题描述
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 (就像在文件版本中一样,您也可以使用LastModified
,CreationDate
或LastAccessed
):
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:
- fileModifiedTime.bat -最后修改具有与设置无关的格式的文件时间.基于 robocopy
- fileTimes.bat -获取文件时间带有 WMIC 的邮票
- dirTimes.bat -获取目录时间带有 WMIC 的邮票
- fileTimesJS.bat -文件 jscript 创建时间戳记
- dirTimesJS.bat -目录 jscript 创建时间戳记
- fileTimesNET.bat - .NET 设置文件时间戳
- dirTimesNET.bat -带有 .NET 的dir时间戳
- fileModifiedTime.bat - gets last modified time of file with settings independent format.Based on robocopy
- fileTimes.bat - gets file time stamps with WMIC
- dirTimes.bat - gets directory time stamps with WMIC
- fileTimesJS.bat - file time stamps with jscript
- dirTimesJS.bat - directory time stamps with jscript
- fileTimesNET.bat - file time stamps with .NET
- dirTimesNET.bat - dir time stamps with .NET
这篇关于如何获取文件或目录的标准化日期/时间戳.在纯批处理脚本中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!