windows 批量格式化文件日期YYYYMMDD

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11083366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:36:44  来源:igfitidea点击:

Format file date YYYYMMDD in batch

windowsbatch-filecmd

提问by Cidr

I've been working on some code in a batch file that evaluates two file dates. If one date is greater than the other then it runs another bat file. What I want to do is format the two dates as YYYYMMDD so that I can use the GTR(greater than).

我一直在处理评估两个文件日期的批处理文件中的一些代码。如果一个日期大于另一个日期,则它运行另一个 bat 文件。我想要做的是将两个日期格式化为 YYYYMMDD,以便我可以使用GTR(大于)。

The code is below but and it works if I use ==(equal) because it's evaluating the string. I only want to know if one file date is greater than the other file date.

代码在下面,但是如果我使用==( equal)就可以工作,因为它正在评估字符串。我只想知道一个文件日期是否大于另一个文件日期。

I'm not asking for someone to amend the code below but if you can show me how to format the dates I would be very grateful.

我不是要求某人修改下面的代码,但如果您能告诉我如何格式化日期,我将不胜感激。



set Fileone=File1.txt
set FileTwo=File2.txt

pushd "D:\Board\Broadcast\FA_Report8_A"
FOR %%f IN (%FileOne%) DO SET filedatetime=%%~tf
FOR %%f IN (%FileTwo%) DO SET filedatetime2=%%~tf
SET filedatetime2=%year%%month%%day%
IF %filedatetime:~0, 10% GTR %filedatetime2:~0, 10% ( 
echo FileOne Greater - run bat
timeout /t 20 /nobreak
goto Finish
) else (
echo FileOne not Greater - Finish
goto Finish
)
:Finish
echo finished 
pause

回答by Alex K.

It's not portable between machines with different date formats but the simplest way is to use a substring: %var:~STARTPOS,LENGTH%

它不能在具有不同日期格式的机器之间移植,但最简单的方法是使用子字符串: %var:~STARTPOS,LENGTH%

set filedatetime=14/06/2012 12:26
set filedatetime=%filedatetime:~6,4%%filedatetime:~3,2%%filedatetime:~0,2%
echo "%filedatetime%"

"20120614"

回答by Aacini

You can separate a date in its parts with a FOR /F command:

您可以使用 FOR /F 命令将日期的各个部分分开:

set filedatetime=14/06/2012 12:26
for /F "tokens=1-3 delims=/ " %%a in ("%filedatetime%") do (
   set filedatetime=%%c%%b%%a
)

This form prevents you to make a mistake in the position or size of each substring, and is very easy to change the order of the parts. For example, if your date is MM/DD/YYYY:

这种形式可以防止你在每个子串的位置或大小上出错,并且很容易改变部分的顺序。例如,如果您的日期是 MM/DD/YYYY:

set filedatetime=%%c%%a%%b

Type FOR /? for further details.

输入 FOR /? 了解更多详情。

回答by It Wasn't Me

The layout for date variable strings in the system can be assumed by settings from the user, by regional and/or language, so, date is not 100% predictable layout to work with.

系统中日期变量字符串的布局可以由用户的设置、区域和/或语言来假设,因此,日期不是 100% 可预测的布局。



Try using wmic OS Get localdatetime /valuebecause the result is 100% predictable:

尝试使用,wmic OS Get localdatetime /value因为结果是 100% 可预测的:

LocalDateTime=20190609123708.733000-180

LocalDateTime=20190609123708.733000-180

SO, if you use in for loop, adding 2 delimiters like =., (equal and dot), you go getting this string output:

所以,如果你在 for 循环中使用,添加 2 个分隔符,如=.,(等于和点),你会得到这个字符串输出:

20190609123708.

20190609123708.

The layout from this command is predictable and works independent of regional settings, user settings or system language, so, the command will always return:

此命令的布局是可预测的,并且与区域设置、用户设置或系统语言无关,因此,该命令将始终返回:

set _date=20190609123708
rem :: %_date:~0,4%_%_date:~4,2%_%_date:~6,2%
rem ::   year:2019 |  month:06  | day:09


In bat file:

在bat文件中:

 @echo off & for /f "tokens=2delims==." %%i in ('wmic OS Get localdatetime /value ^|findstr /r [0-9]')do set "_date=%%i" & echo/%_date:~0,4%%_date:~4,2%%_date:~6,2%

In command line:

在命令行中:

for /f "tokens=2delims==." %i in ('wmic OS Get localdatetime /value ^|findstr /r [0-9]')do set "_data=%i" & mkdir %_date:~0,4%_%_date:~4,2%_%_date:~6,2%


This commands result ::

此命令结果 ::

    20190609




May I can also suggest:

我还可以建议:

wmic Path Win32_LocalTime Get Day,Month,Year 

In looping for:

在循环中:

@echo off & setlocal enabledelayedexpansion & set "_do=wmic Path Win32_LocalTime Get Day^,Month^,Year" 
for /f "tokens=1-3delims= " %%a in ('!_do!^|findstr /r [0-9]')do set "y=%%c" & set "d=0%%a" & set "m=0%%b"
echo/!y!!m:~-2!!d:~-2! >nul 

Result:

结果:

Day  Month  Year
 9   6      2019 
%%a  %%b    %%c

The difference is no zero in number for month/day less equal 9, so, you can use this bat to put leading zero in this case:

月/日的差值不为零,小于等于 9,因此,在这种情况下,您可以使用此 bat 放置前导零:

@echo off & setlocal enabledelayedexpansion & set "_do=wmic Path Win32_LocalTime Get Day^,Month^,Year" 
for /f "tokens=1-3delims= " %%a in ('!_do!^|findstr /r [0-9]')do set "y=%%c" & set "d=0%%a" & set "m=0%%b"
echo/!y!!m:~-2!!d:~-2!

Result in YearMonthDay:

YearMonthDay 的结果:

20190609


Obs.:In PowerShell, the layout can be customized simple by:

观察:PowerShell 中,可以通过以下方式简单地自定义布局:

ToString("yyyyMMdd")

ToString("yyyyMMdd")

The stringscan be set with ToString.

字符串可以与设置ToString

Sample:yyyy-MM-dd, dd-MM-yyyy, MM-dd-yyyy, MM_dd_yyyy, yyyy_MM_dd, etc..

样本:yyyy-MM-dd, dd-MM-yyyy, MM-dd-yyyy, MM_dd_yyyy, yyyy_MM_dd, etc..



The Powershell command:

Powershell 命令:

$(Get-Date).ToString("yyyyMMdd")

Result:

结果:

2010609


See more about date variable layout output in batch here:

在此处查看有关批处理日期变量布局输出的更多信息:

Safe way to get current day month and year in batch

Parsing Dates in Batch Files & Regional Settings / Locale

批量获取当前日期月份和年份的安全方法

在批处理文件和区域设置/区域设置中解析日期





Update- How applying the comments/observations above in your bat, so, if I understood your code in this question:

更新- 如何在你的蝙蝠中应用上面的评论/观察,所以,如果我理解你在这个问题中的代码:



@echo off & setlocal enabledelayedexpansion 

set "Fileone=File1.txt" &  set "FileTwo=File2.txt"

set "_both=!FileOne!-!FileTwo!" & rem cd /d "D:\Board\Broadcast\FA_Report8_A"

set "_path_back=%__CD__%" & rem :: you can use this "%__CD__%" or pushd "D:\Board\Broadcast\FA_Report8_A" 

for /f "tokens=2delims==." %%i in ('wmic OS Get localdatetime /value ^|findstr /r [0-9]')do set "_date=%%i"

set _now=!_date:~0,4!!_date:~4,2!!_date:~6,2! & for /f "tokens=1*delims=-" %%i in ('echo/!_both!')do (

     call :compare "%%~fi" & call :compare "%%~fj" & if !_dt_file_2! gtr !_dt_file_2! ( 

         echo/ FileOne Greater - run bat & timeout /t 20 /nobreak & goto :run_bat

         ) else (echo/ FileOne not Greater - Finish & goto Finish) 

    )

:compare
set "_file="  & set "_file=%~1" 

for /f "tokens=1delims=. " %%d in ('wmic datafile where name^='!_file:\=\!' get LastModified ^|findstr /v "LastModified"')do (

    if "!_dt_file_1!./" == "./" (set _dt_file_2=%%d) else (set _dt_file_2=%%d)

    ) & exit /b

:run_bat 
call the_bat_to_run.cmd 

:Finish
echo/Finished


So sorry by my limitedEnglish...

很抱歉我的英语有限...