Windows命令行中是否有一种方法可以以独立于标准语言环境的格式(例如ISO8601)来检索文件或目录的日期/时间戳(修改,创建,访问)?
我发现使用的输出dir
依赖于系统的日期/时间格式,而缺少秒。参数()和变量扩展()
的~t
修饰符也是如此。另外,变量和依赖于系统设置(尽管后者也返回至少秒)。%~t1
for
%~tI
forfiles
@fdate
@ftime
我了解到这wmic OS GET LocalDateTime /VALUE
是一种获取当前系统日期/时间的方法(输出可能看起来像LocalDateTime=20151021020000.000000+120
,可以通过捕获for /F
)。
但是是否还有一条命令(行)以类似的格式返回文件或目录的日期/时间戳?
在这里,您可以找到获取文件时间的方法。
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"));
编辑这里准备使用列出的所有方法来使用参数化脚本:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句