批处理文件,用于检查当前月份中创建的文件是否存在

时间:2016-02-22 13:49:38

标签: windows batch-file

我需要一个批处理文件来检查特定文件夹中是否存在上次修改日期为当前月份的文件。年是否存在。

Ex:本月显示FEB-16 (02/2016);(日期对我来说无关紧要)

我可以输入文件夹/文件名,因为它是静态的。

1 个答案:

答案 0 :(得分:0)

您可以使用:

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%"
set /p "file=Enter your file here: "
if not exist "%file%" (
    echo file %file% not found
    goto end
    )
FOR %%i IN ("%file%") DO (
    for /f "tokens=2,3 delims=-: " %%g in ("%%~ti") do (
        if "%%h%%g"=="%YYYY%%MM%" (
            echo %%i is from this month!
            )
        if not "%%h%%g"=="%YYYY%%MM%" (
            echo %%i is not from this month!
            )
        )
    )
:end
pause

修改

区域设置非特定版本:

@echo off
setlocal EnableDelayedExpansion
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%"
set /p "file=Enter your file here: "
if not exist "%file%" (
    echo file %file% not found
    goto end
    )
FOR %%i IN ("%file%") DO (
    set "escapedFile=%%~dpnxi"
    set "escapedFile=!escapedFile:\=\\!"
    for /f "tokens=2 delims==" %%a in ('wmic datafile where name^="!escapedFile!" get LastModified /value') do set "dt2=%%a"
    set "YYYY2=!dt2:~0,4!" & set "MM2=!dt2:~4,2!"
    if "!YYYY2!-!MM2!"=="!YYYY!-!MM!" (
        echo %%i is from this month!
        )
        if not "!YYYY2!-!MM2!"=="!YYYY!-!MM!" (
        echo %%i is not from this month!
        )
    )
:end
pause