编写一个批处理文件的日期和时间明智以除去夹 [英] write a batch file to remove the folders by date and time wise

查看:200
本文介绍了编写一个批处理文件的日期和时间明智以除去夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确切的重复:

我有一个名为倾倒在网络上的服务器文件夹。每天都有大量的临时文件和文件夹将被保存的文件夹中。

我想删除这些文件和子文件夹。条件是我想要的文件和功放之前删除一小时文件夹只即我们的一个,当我们正在运行的批处理文件,在当前时间之前。

这意味着我要保留一个我们的文件夹中运行其他活动。
这样当前的时间。


  1. 前往文件夹并列出文件随着时间的明智

  2. 获取当前时间

  3. 删除均高于当前的时间
  4. 年长1小时的文件

解决方案

我想你最好在这种情况下使用PowerShell的或WSH(如Rihan Meij提到)。比较数据和时间是可能的,但非常脆(因为没有办法让在批次文化中立数据/时间)。你可能会这样写:

  SETLOCAL ENABLEEXTENSIONS
现在设置=%日期%
设置NowTime =%的时间:〜0,-3%
拨打:tokenize_date%现在%
拨打:tokenize_time%NowTime%
回声%YEAR% - %MONTH% - %DAY%T%HOUR%:%MINUTE%:%第二%
拨打:one_hour_before%现在%% NowTime%
回声%RESULT_DATE%%RESULT_TIME%
平-n 1 -w 1500 :: 1> NUL
设置Now2 =%日期%
设置NowTime2 =%的时间:〜0,-3%
拨打:date_time_compare%现在%% NowTime%%Now2%%NowTime2%
回声%现在%T相比%Now2%T%NowTime2%%NowTime%:%RESULT%
ENDLOCAL
GOTO:EOF:tokenize_date
    REM假设ISO 8601日期格式YYYY-MM-DD
    SETLOCAL ENABLEEXTENSIONS
    设置ARG =%1
    设置年=%ARG:〜0.4%
    设置MONTH =%ARG:5,2〜%
    设置DAY =%ARG:8.2〜%
    ENDLOCAL&放大器;设置年=%YEAR%放大器;设置月=%MONTH%放大器;设置DAY = DAY%%
GOTO:EOF:tokenize_time
    REM假设ISO-8601时间格式HH:MM:SS,24小时
    SETLOCAL ENABLEEXTENSIONS
    设置ARG =%1
    集小时=%ARG:0,2〜%
    集分钟=%ARG:〜3,2%
    一套第二=%ARG:6,2〜%
    ENDLOCAL&放大器;设置小时=%HOUR%放大器;设定分钟=%MINUTE%放大器;一套第二=%SECOND%
GOTO:EOF:one_hour_before
    REM得到一个日期%1和时间2%
    REM返回RESULT_DATE日期和RESULT_TIME时间
    SETLOCAL enabledelayedexpansion ENABLEEXTENSIONS
    组个月= 31 28 31 30 31 30 31 31 30 31 30 31
    集I = 0
    在%% X(%几个月%)做(
        集/ A I + = 1
        集month_length_!我!= %%点¯x
    )
    致电:tokenize_date 1%
    拨打电话:tokenize_time 2%
    集/一个小时 - = 1
    如果HOUR%LSS%0(
        集/一按日期= 1
        集/一小时+ = 24
        如果DAY%LSS%0(
            集/一个为期一个月= 1
            如果%,当月%LSS 0(
                集/ A年 - = 1
                集/ A +月= 12
            )
            套/天+ = month_length_!MONTH!
        )
    )
    ENDLOCAL&放大器;设置RESULT_DATE =%YEAR% - %MONTH% - %DAY%放大器;设置RESULT_TIME =%HOUR%:%MINUTE%:%第二%
GOTO:EOF:date_time_compare
    预计REM%1 - 日期1,%2 - 时间1%3 - 日期2%4 - 时间2
    REM返回一个值< 0,如果datetime1比DATETIME2更早
    REM> 0比晚
    REM = 0等于
    REM的返回值被保存在变量%的回报率%
    SETLOCAL ENABLEEXTENSIONS
    拨打:tokenize_date 3%
    拨打:tokenize_time 4%
    设置YEAR2 =%,比去年%
    设置MONTH2 =%MONTH%
    设置DAY2 =%DAY%
    设置HOUR2 =%HOUR%
    设置MINUTE2 =%MINUTE%
    设置秒2 =%SECOND%
    致电:tokenize_date 1%
    拨打电话:tokenize_time 2%
    如果YEAR2%GTR%%,比去年%(ENDLOCAL&安培;设置RESULT = -1&放大器;转到:EOF)其他(
        如果YEAR2%LSS%%,比去年%(ENDLOCAL&安培;设置RESULT = 1&安培;转到:EOF)其他(
            如果MONTH2%%% DAY2 %% HOUR2 %% MINUTE2 %%秒2%GTR%,当月%%日%% HOUR %% MINUTE %%第二个%(
                ENDLOCAL&放大器;设置RESULT = -1&放大器;转到:EOF
            )其他(
                如果MONTH2%%% DAY2 %% HOUR2 %% MINUTE2 %%秒2%==%,当月%%日%% HOUR %% MINUTE %%第二个%(
                    ENDLOCAL&放大器;设置结果= 0&放大器;转到:EOF
                )其他(
                    ENDLOCAL&放大器;设置RESULT = 1&安培;转到:EOF
                )
            )
        )
    )
GOTO:EOF

对不起,它已经变得有点长。比较日期/时间只得到部分现在测试,但应该工作。小心,当它含有亚秒级,因为那些在这里介绍一个逗号扰乱参数顺序调用的子程序(因此%的时间的时候,:〜0,-3%避免)。

之后,你可以使用%〜TI 扩张来获得一个日期和时间,把它们比作当前减去一小时,如果适用删除。

注1:我假设ISO 24小时,这是我在这里使用8601日期和时间格式。对于非标准的日期格式,你将不得不改变一个子程序位(但12小时制时间可能会是一个痛苦)。

注2:code不关心闰年,目前。将是另一个5或6号线code的:one_hour_before ,我觉得

Exact Duplicates:

I have a folder named 'dump' on a sever on a network. Every day lot of temp files and folders will be saved in that folder.

I want to delete those files and sub folders. The condition is that I want to delete one hour before files & folders only i.e one our before the current time when we are running the batch file.

This means I want to keep one our files in that folder to run the other ACTIVITIES. like this current time.

  1. go to folder and list the files as time wise
  2. get the current time
  3. delete the files which were 1 hour older than current time

解决方案

I think you are better off using PowerShell or WSH (as Rihan Meij mentioned) in this case. Comparing data and time is possible but very brittle (as there is no way to get a data/time in batch culturally-neutral). You might write something like this:

setlocal enableextensions
set Now=%date%
set NowTime=%time:~0,-3%
call :tokenize_date %Now%
call :tokenize_time %NowTime%
echo %YEAR%-%MONTH%-%DAY%T%HOUR%:%MINUTE%:%SECOND%
call :one_hour_before %Now% %NowTime%
echo %RESULT_DATE% %RESULT_TIME%
ping -n 1 -w 1500 ::1>NUL
set Now2=%date%
set NowTime2=%time:~0,-3%
call :date_time_compare %Now% %NowTime% %Now2% %NowTime2%
echo %Now%T%NowTime% compared to %Now2%T%NowTime2%: %RESULT%
endlocal
goto :eof

:tokenize_date
    rem assuming ISO 8601 date format YYYY-MM-DD
    setlocal enableextensions
    set arg=%1
    set YEAR=%arg:~0,4%
    set MONTH=%arg:~5,2%
    set DAY=%arg:~8,2%
    endlocal&set YEAR=%YEAR%&set MONTH=%MONTH%&set DAY=%DAY%
goto :eof

:tokenize_time
    rem assuming ISO-8601 time format HH:MM:SS, 24-hour
    setlocal enableextensions
    set arg=%1
    set HOUR=%arg:~0,2%
    set MINUTE=%arg:~3,2%
    set SECOND=%arg:~6,2%
    endlocal&set HOUR=%HOUR%&set MINUTE=%MINUTE%&set SECOND=%SECOND%
goto :eof

:one_hour_before
    rem gets a date as %1 and a time as %2
    rem returns a date in RESULT_DATE and a time in RESULT_TIME
    setlocal enabledelayedexpansion enableextensions
    set months=31 28 31 30 31 30 31 31 30 31 30 31
    set i=0
    for %%x in (%months%) do (
        set /a i+=1
        set month_length_!i!=%%x
    )
    call :tokenize_date %1
    call :tokenize_time %2
    set /a HOUR-=1
    if %HOUR% LSS 0 (
        set /a DAY-=1
        set /a HOUR+=24
        if %DAY% LSS 0 (
            set /a MONTH-=1
            if %MONTH% LSS 0 (
                set /a YEAR-=1
                set /a MONTH+=12
            )
            set /a DAY+=month_length_!MONTH!
        )
    )
    endlocal&set RESULT_DATE=%YEAR%-%MONTH%-%DAY%&set RESULT_TIME=%HOUR%:%MINUTE%:%SECOND%
goto :eof

:date_time_compare
    rem expects %1 - date 1, %2 - time 1, %3 - date 2, %4 - time 2
    rem returns a value < 0 if datetime1 is earlier than datetime2
    rem                 > 0                 later than
    rem                 = 0                 equal to
    rem the return value is saved in the variable %RETURN%
    setlocal enableextensions
    call :tokenize_date %3
    call :tokenize_time %4
    set YEAR2=%YEAR%
    set MONTH2=%MONTH%
    set DAY2=%DAY%
    set HOUR2=%HOUR%
    set MINUTE2=%MINUTE%
    set SECOND2=%SECOND%
    call :tokenize_date %1
    call :tokenize_time %2
    if %YEAR2% GTR %YEAR% (endlocal&set RESULT=-1&goto :eof) else (
        if %YEAR2% LSS %YEAR% (endlocal&set RESULT=1&goto :eof) else (
            if %MONTH2%%DAY2%%HOUR2%%MINUTE2%%SECOND2% GTR %MONTH%%DAY%%HOUR%%MINUTE%%SECOND% (
                endlocal&set RESULT=-1&goto :eof
            ) else (
                if %MONTH2%%DAY2%%HOUR2%%MINUTE2%%SECOND2%==%MONTH%%DAY%%HOUR%%MINUTE%%SECOND% (
                    endlocal&set RESULT=0&goto :eof
                ) else (
                    endlocal&set RESULT=1&goto :eof
                )
            )
        )
    )
goto :eof

Sorry, it has gotten a bit long. Comparing date/time has only partially been tested right now, but should work. Careful with the time when it contains sub-seconds, since those introduce a comma here, which disturbs the argument order for called subroutines (hence the %time:~0,-3% to avoid that).

After that you can use the %~tI expansion to get a date and a time, compare them to current minus one hour and delete if applicable.

Note 1: I am assuming ISO 8601 date and time formats with 24 hours, which is what I am using here. For non-standard date formats you would have to alter the subroutines a bit (but 12 hour time would probably be a pain).

Note 2: The code doesn't care about leap years, currently. Would be another 5 or 6 lines of code in :one_hour_before, I think.

这篇关于编写一个批处理文件的日期和时间明智以除去夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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