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?
-
IMO this question belongs to serverfault– onofCommented Jul 27, 2011 at 6:28
-
you mean creation time stamp or modification time stamp?– PA.Commented Jul 27, 2011 at 7:41
-
@onof, IMO this is a question about programming bat files.– PA.Commented Jul 27, 2011 at 13:55
-
This is a task much easier done in PowerShell, if you have access to it.– alecovCommented Jul 30, 2013 at 18:23
3 Answers
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
-
1Can't say I think much of temporarily updating registry settings like this. Could cause problems for other running apps.– JoeCommented Apr 23, 2013 at 19:37
-
2Avoid of registry manipulations, use
wmic
. Try next from command line:for /F "tokens=*" %G in ('wmic os get localdatetime^|find "."') do @echo =%G
. Returns system local date & time in a handyyyyymmddhhmmss...
format.– JosefZCommented Jan 29, 2015 at 12:39 -
~t is date modified. Is it possible to get date created? Commented Jul 13, 2019 at 1:26
-
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.
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"