在Windows命令行中,有没有一种方法可以以标准化的区域独立格式(例如ISO8601 )检索文件或目录的date/时间戳记(修改,创build,访问)?
我发现dir
的输出使用系统相关的date/时间格式,缺less秒钟。
参数( %~t1
)和variables扩展( %~tI
)的%~tI
修饰符也是如此。
此外, forfiles
variables@fdate
和@ftime
取决于系统设置(尽pipe后者也返回至less秒)。
我已经了解到, wmic OS GET LocalDateTime /VALUE
是获取当前系统date/时间的一种方法(输出可能看起来像LocalDateTime=20151021020000.000000+120
,它可以被for /F
捕获)。
但是是否还有一个命令(行),以相似的格式返回文件或目录date/时间戳?
在这里你可以找到一个方法来获取文件的时间。
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混合 (就像在文件版本中,您也可以使用LastModified
, CreationDate
或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格式化功能,它可以是最强大的选项):
@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"));
编辑这里准备使用参数化脚本使用所有列出的方法: