本文介绍了如何批量获取文件的与语言环境无关的修改时间和创建时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

批量变量/参数扩展类似%~t1 可以获取文件的时间戳.

With batch variable/parameter expansion like %~t1 one can get a timestamp of a file.

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

I would like to set the year of the file to another variable would like to support multiple locales.

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

How can you get a file's datetime, independent of locale and regional settings? No powershell please.

推荐答案

我会发布几个选项

1)第一个是 wmic(不适用于 XP 家庭版)(LastModified 可以用 CreationDateLastAccessed )

1)First one is with wmic (not available for XP home edition) (LastModified can be changed with CreationDate or LastAccessed )

@echo off

set file_loc=.	emp_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 hybrid (DateLastModified 可以改成 DateCreatedDateLastAccessed 时间格式可以随便改你想要):

2). Jscript/.bat hybrid (DateLastModified can be changed to DateCreated or DateLastAccessed .Time format can be changed to whatever you want ):

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

set file_loc=.	emp_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) selfcompiled jscript.net/bat hybrid(GetLastWriteTime可以改为GetLastAccessTimeGetCreationTime.时间格式可以更改):

3) selfcompiled jscript.net/bat hybrid (GetLastWriteTime can be changed to GetLastAccessTime or GetCreationTime . Time format can be changed) :

@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.NETFramework"
:: searching the latest installed .net framework
for /f "tokens=* delims=" %%v in ('dir /b /s /a:d /o:-n "%SystemRoot%Microsoft.NETFrameworkv*"') do (
    if exist "%%vjsc.exe" (
        rem :: the javascript.net compiler
        set "jsc=%%~dpsnfxvjsc.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=.	emp_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 这个可以用...

4). robocopy - with this you can get only last modified date (with other methods you can get all time attributes).As time stamps in robocopy are always YYYY/MM/DD HH:mm:SS this can be used...

@ECHO OFF

set file_loc=.	emp_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

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

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

  1. fileModifiedTime.bat - 获取最后修改时间具有设置独立格式的文件.基于 robocopy
  2. fileTimes.bat - 获取文件时间戳使用 WMIC
  3. dirTimes.bat - 获取目录时间戳使用 WMIC
  4. fileTimesJS.bat - 文件时间jscript
  5. 标记
  6. dirTimesJS.bat - 目录时间jscript
  7. 标记
  8. fileTimesNET.bat - 文件.NET
  9. 的时间戳
  10. dirTimesNET.bat - 目录.NET
  11. 的时间戳
  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

这篇关于如何批量获取文件的与语言环境无关的修改时间和创建时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 06:23