FORFILES命令删除12小时.bat文件旧文件 [英] Forfiles command to delete 12 hours old files in .bat file

查看:1277
本文介绍了FORFILES命令删除12小时.bat文件旧文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道命令删除它们olders接天的文件。
如何使用删除12小时旧文件FORFILES命令

I know the command to delete the files which are olders then days. How to delete 12 hours old files using forfiles command

我在.bat文件中使用forfile命令

I am using forfile command in .bat file

推荐答案

好吧,我也为你努力工作......

Ok, I did hard work for you...

SETLOCAL ENABLEDELAYEDEXPANSION
REM 1: Parse date time string
FOR /F "TOKENS=1-5 DELIMS=/-:,. " %%i IN ("%DATE% %TIME%") DO (
    SET year=%%k
    SET month=%%j
    SET day=%%i
    SET hour=%%l
    SET minute=%%m
)
REM 2: Subtract 12 hours, handling day wrap including leap year.
SET /A hour-=12
IF %hour% LSS 0 SET /A hour+=24 & SET /A day-=1
IF %day% LSS 1 (
   SET /A month-=1
   IF !month! LSS 1 SET /A month+=12 & SET /A year-=1
   FOR %%m IN (1 3 5 7 8 10 12) DO IF %%m == !month! SET /A day+=31
   FOR %%m IN (4 6 9 11) DO IF %%m == !month! SET /A day+=30
   IF !month! == 2 (
    SET /A day+=28
    SET /A y4=!year! %% 4
        IF !y4! == 0 (
        SET /A y400=!year! %% 400
        IF !y400! == 0 (
            SET /A day+=1
        ) ELSE (
            SET /A y100=!year! %% 100
            IF NOT !y100! == 0 SET /A day+=1
        )
    )
    )
)
REM 3: Normalize string for later string comparison
CALL :norm_datetime "%day%-%month%-%year% %hour%:%minute%" limit_datetime
REM 4: List file, parse file sate time and delete on comparison verified.
FOR %%f IN (*.*) DO (
    CALL :norm_datetime "%%~tf" file_datetime
    IF !file_datetime! LSS %limit_datetime% ECHO DEL %%f
)
ENDLOCAL
GOTO :EOF

REM 4: function to normalize date time string
REM norm_datetime "time string" output_environment_variable
REM time string format must be d-m-y-h-m[-...], where - is any valid separator.
REM output will be YYYYMMDDTHHMM
:norm_datetime
FOR /F "TOKENS=1-5 DELIMS=/-:,. " %%i IN ("%~1") DO (
    SET year=0000%%k
    SET month=00%%j
    SET day=00%%i
    SET hour=00%%l
    SET minute=00%%m
)
SET %2=%year:~-4%%month:~-2%%day:~-2%T%hour:~-2%%minute:~-2%
GOTO :EOF

检查预期的日期和时间格式在code的言论。

Check expected date and time formats in code remarks.

请注意,日期和时间格式取决于系统,所以这code必须是适用于不同的系统。

Note that date and time format are system dependent, so this code must be adapted for different systems.

这篇关于FORFILES命令删除12小时.bat文件旧文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆