How can I check the time stamp creation of a file in a Windows batch script?

前端 未结 3 1250
感动是毒
感动是毒 2020-12-03 23:45

I need help how do check files in particular folders with time stamp older than 2 days then will remove or delete or copy to other place?

相关标签:
3条回答
  • 2020-12-04 00:25

    Not sure about deleting, but you can use RoboCopy (which is part of Windows 7). The parameter: /MAXAGE:n will copy the files older than n - I normally do a copy to a backup folder and later I do a "delete" all from that directory once I'm sure.

    Hope this helps.

    0 讨论(0)
  • 2020-12-04 00:31

    A simple FOR loop with a SET command using the ~t modifier returns the last-modified date of the files in a directory.

    See this example

    @echo off
    setlocal enabledelayedexpansion
    echo Files changed today %date%
    FOR %%A IN (*.*) DO (
      set tf=%%~tA
      set fd=!tf:~0,10!
      if !fd!==%date% (
        echo  %%F !tf! 
      )
    )
    

    See HELP FOR and HELP SET for detailed information.

    But, for comparing dates beyond the simple comparison showed above, you need to extract each date component

    set dd=!tf:~0,2!
    set mm=!tf:~3,2!
    set yyyy=!tf:~6,4!
    

    But, wait, extracting the date components in a BAT file is a very tricky issue, because %DATE% and the ~t modifier format the date using the short-date format, that is fully (endlessly) customizable. One user may configure its system to return Fri040811 while another user may choose 08/04/2011. It's a complete nightmare for a BAT programmer.

    One possible solution is to temporarily change the format. See this example.

    @echo off
    echo System Date Time = %date% %time%
    reg copy "HKCU\Control Panel\International" "HKCU\Control Panel\International-Temp" /f >nul
    reg add "HKCU\Control Panel\International" /v sShortDate /d "yyyy-MM-dd" /f >nul
    reg add "HKCU\Control Panel\International" /v sTimeFormat /d "HH:mm:ss" /f >nul
    echo Normalized Date Time = %date% %time%
    set dd=%date:~8,2%
    set mm=%date:~5,2%
    set yyyy=%date:~0,4%
    reg copy "HKCU\Control Panel\International-Temp" "HKCU\Control Panel\International" /f >nul
    

    And finally you should do the arithmetic with dates, you need to transform the date in DD MM YYYY to a number of days, which is not obvious neither. Here is some code to do this transformation.

    :days
    :: Algorithm based on Fliegel-Van Flandern algorithm from the Astronomical Almanac,
    :: provided by Doctor Fenton on the Math Forum (http://mathforum.org/library/drmath/view/51907.html),
    :: and converted to batch code by Ron Bakowski.
    SET /A Month1 = ( 1%MM% %% 100 - 14 ) / 12
    SET /A Year1  = %YYYY% + 4800
    SET /A days = 1461 * ( %Year1% + %Month1% ) / 4 + 367 * ( (1%MM% %% 100) - 2 -12 * %Month1% ) / 12 - ( 3 * ( ( %Year1% + %Month1% + 100 ) / 100 ) ) / 4 + (1%DD% %% 100) - 32075
    SET Month1=
    SET Year1=
    goto :eof
    

    the strange idiom (1%MM% %% 100) is used to fix a problem with the way SET /A interprets as octal the numbers that begin with zero.

    so, putting all those pieces together...

    @echo off
    setlocal enabledelayedexpansion enableextensions
    
    reg copy "HKCU\Control Panel\International" "HKCU\Control Panel\International-Temp" /f >nul
    reg add "HKCU\Control Panel\International" /v sShortDate /d "yyyy-MM-dd" /f >nul
    reg add "HKCU\Control Panel\International" /v sTimeFormat /d "HH:mm:ss" /f >nul
    
    set dd=%date:~8,2%
    set mm=%date:~5,2%
    set yyyy=%date:~0,4%
    call :days
    set /a today=!days!
    
    FOR %%A IN (*.*) DO (
      set tf=%%~tA
      set fd=!tf:~0,10!
      set dd=!fd:~8,2!
      set mm=!fd:~5,2!
      set yyyy=!fd:~0,4!
      call :days
      set /a age= !today!-!days!
      if !age! leq 2 (
        echo  %%A is !age! days old
      )
    )
    reg copy "HKCU\Control Panel\International-Temp" "HKCU\Control Panel\International" /f >nul
    goto :eof
    
    :days
    :: Algorithm based on Fliegel-Van Flandern algorithm from the Astronomical Almanac,
    :: provided by Doctor Fenton on the Math Forum (http://mathforum.org/library/drmath/view/51907.html),
    :: and converted to batch code by Ron Bakowski.
    SET /A Month1 = ( 1%MM% %% 100 - 14 ) / 12
    SET /A Year1  = %YYYY% + 4800
    SET /A days = 1461 * ( %Year1% + %Month1% ) / 4 + 367 * ( (1%MM% %% 100) - 2 -12 * %Month1% ) / 12 - ( 3 * ( ( %Year1% + %Month1% + 100 ) / 100 ) ) / 4 + (1%DD% %% 100) - 32075
    SET Month1=
    SET Year1=
    goto :eof
    
    0 讨论(0)
  • 2020-12-04 00:43

    here is the reference of how you can delete files older than 2 days

    following command on cmd will do it.

    forfiles  /p "c:\path" /s /m *.* /d -365 /c "cmd /c del @file"
    
    0 讨论(0)
提交回复
热议问题