2

通过批量变量/参数扩展%~t1可以获得文件的时间戳。

我想将文件的年份设置为另一个希望支持多个语言环境的变量。

如何获得文件的日期时间,而不受语言环境和区域设置的影响?请不要使用powershell。

4

1 回答 1

4

我会发布几个选项

1)第一个是withwmic(不适用于XP家庭版)(LastModified可以用CreationDateor更改LastAccessed

@echo off

set file_loc=.\temp_file
for %%# in ("%file_loc%") do set file_loc=%%~dpfnx#
set file_loc=%file_loc:\=\\%


for /f "delims=." %%t in ('"WMIC DATAFILE WHERE name="%file_loc%" get LastModified /format:value"') do (
    for /f %%$ in ("%%t") do if "%%$" neq "" set %%$
)

echo %LastModified%
echo year : %LastModified:~0,4%
echo month : %LastModified:~4,2%
echo day : %LastModified:~6,2%

2)。Jscript/.bat 混合DateLastModified可以更改为DateCreatedDateLastAccessed.Time 格式可以更改为您想要的任何格式):

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

set file_loc=.\temp_file
for %%# in ("%file_loc%") do set file_loc=%%~dpfnx#
::set file_loc=%file_loc:\=\\%
cscript //E:JScript //nologo "%~f0" "%file_loc%"
exit /b 0

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

var file_loc = WScript.Arguments.Item(0);
var fso = new ActiveXObject("Scripting.FileSystemObject");
var the_file=fso.GetFile(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/bat hybridGetLastWriteTime可以改成 GetLastAccessTimeGetCreationTime.时间格式可以改):

@if (@X)==(@Y) @end /****** silent line that start 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 file_loc=.\temp_file
for %%# in ("%file_loc%") do set file_loc=%%~dpfnx#


"%~n0.exe" "%file_loc%"


exit /b 0


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

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

var the_file=arguments[1];
var last_modified=File.GetLastWriteTime(the_file);
 Console.WriteLine(last_modified.ToString("yyyy-MM-dd"));

4)。robocopy - 有了这个,你只能得到最后修改的日期(用其他方法你可以得到所有时间属性)。因为 robocopy 中的时间戳总是YYYY/MM/DD HH:mm:SS可以使用的......

@ECHO OFF

set file_loc=.\temp_file
for %%# in ("%file_loc%") do set file_dir=%%~dp#
for %%# in ("%file_loc%") do set file_name=%%~nx#
pushd %file_dir%

for /f "tokens=1,2" %%a in ('robocopy  "." "%temp%" /l /fat /ts /LEV:1 /NP /NC /NS /NJS /NJH^|findstr /i /e /c:"%file_name%"') do (
    echo %%a %%b
)
popd

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

  1. fileModifiedTime.bat - 使用设置独立格式获取文件的最后修改时间。基于robocopy
  2. fileTimes.bat - 使用WMIC获取文件时间戳
  3. dirTimes.bat - 使用WMIC 获取目录时间戳
  4. fileTimesJS.bat - 带有jscript的文件时间戳
  5. dirTimesJS.bat - 带有jscript 的目录时间戳
  6. fileTimesNET.bat - .NET文件时间戳
  7. dirTimesNET.bat - .NET 的 dir 时间戳
于 2014-09-02T20:43:28.253 回答