# Batch File



## elroth (Feb 13, 2008)

Hi ,

I am quiet new to batch files , so i would be helpful if anyone can give me solution to this problem.
i need a batch file which would delete a file based on its date modified , i.e. if the modified date is older than 3 months , it would be deleted. the files are in D:\New\Mine\K folder. the date format is (dd/mm/yyyy) . This should be checked with the current date , and if it is older then 3 months, it should be deleted.

I need this fix urgently.If anyone can help, i would really obliged

Thanks 
Elroth


----------



## devil_himself (Apr 7, 2007)

Try This Script ... I'm Not The Author, Credited In File

Note :- Not Tested


```
::PurgeOld.bat
::Author = Patrick J. O'Brien
::Date = 2/15/01
::
:: modified to Purge.bat by Paul Schwartz 3/7/04
:: Added command to clear attributes of hidden, system, read only files.
:: This batch file will show no mercy.  Use with caution.

@echo off
if .%3.==.. goto Syntax

::days to subtract
set DaysOld=%3

::path to purge
set P2Purge=%1

::File mask to purge
set F2Purge=%2
if not exist %P2Purge%\%F2Purge% echo %P2Purge%\%F2Purge% not found! & goto :eof

::Get todays date
call :mmddnt

::Calculate CutOffDate var
set /a CutOffDate=%Year%-1995
set /a CutOffDate=%CutOffDate%*365
set /a TempVar=%Month%-1
set /a TempVar=%TempVar%*31
set /a CutOffDate=%CutOffDate% + %TempVar%
set /a CutOffDate=%CutOffDate% + %Day% - %DaysOld%
set TempVar=

for /f "tokens=1-6 delims=/ " %%i in ('dir /a-d /-c %P2Purge%\%F2Purge% ^|find "/"') do call :CheckAndPurge %%i %%j %%k %%l %%m %%n


goto :eof


:CheckAndPurge

if .%6.==.. goto :eof

::echo %1 %2 %3 %4 %5 %6
Set TestMo=%1
if .%TestMo%.==.08. set TestMo=8
if .%TestMo%.==.09. set TestMo=9

Set TestDay=%2
if .%TestDay%.==.08. set TestDay=8
if .%TestDay%.==.09. set TestDay=9

set /a TestMo=%TestMo%
set /a TestDay=%TestDay%

if %3 LSS 50 set /a TestYear=%3
if %3 GTR 49 set /a TestYear=%3

set /a TestDate=%TestYear%-1995
set /a TestDate=%TestDate%*365
set /a TestMo=%TestMo%-1
set /a TestMo=%TestMo%*31
set /a TestDate=%TestDate%+%TestMo%+%TestDay%

::AFTER TESTING ... THIS IS WHERE YOU HAVE TO CHANGE
::echo %6 testdate= %TestDate%  CutOffDate= %CutOffDate%
if %TestDate% LSS %CutOffDate% attrib -R -A -S -H %P2Purge%\%6
if %TestDate% LSS %CutOffDate% echo %P2Purge%\%6 has been deleted.
if %TestDate% LSS %CutOffDate% del %P2Purge%\%6
::after testing thoroughly change the "xxxdel" to just "del" and remove the "::" at the start of the line

goto :eof

:mmddnt

for /F "tokens=1-4 delims=/ " %%i in ('date /t') do (
   set DayOfWeek=%%i
   set Month=%%j
   set Day=%%k
   set Year=%%l
   set Date=%%i %%j/%%k/%%l
)

::change from invalid octals 8 and 9
If .%Month%.==.08. set Month=8
If .%Month%.==.09. set Month=9

If .%day%.==.08. set Day=8
If .%Day%.==.09. set Day=9

Set /a Month=%Month%
Set /a Day=%Day%


goto :eof

:Syntax

echo.
echo usage:    Purge  Path  FileMask  Xdays
echo.
echo       where Path is the path and FileMask is the file name(s) (wild cards ok)
echo             Xdays is the number of days old to be deleted
echo.
```


----------



## Squashman (Apr 4, 2003)

I kow this is going to sound funny but you can actually use XXcopy to do this as well with one line of code.


----------



## Squashman (Apr 4, 2003)

Couldn't they use FORFILES from the 2003 resource kit which works on XP. Seems like that would be much easier.
ftp://ftp.microsoft.com/ResKit/y2kfix/x86/


----------



## ChuckE (Aug 30, 2004)

While batch commands are fun to play with, and I've been around since before any version of Windows, 
and I am quite (not "quiet") used to making batch files, why try to learn something when 
Windows Explorer already has the ability to search for files between dates?

If you do such a modified search, and that is very easy, just delete from that list of found hits.


----------



## Squashman (Apr 4, 2003)

Very true ChuckE. But what if you wanted to automate this mundane task to run everyday or once a week.


----------



## ChuckE (Aug 30, 2004)

You can save a search, and you could set up an "at" command to run the search on some schedule.

As for automating to a point where matching files get deleted without intervention, I would not like that. But I suppose batching might have a edge there, if that is what the user wants to do. We don't know that.

To do that with usual Windows Searching, I think could be done. But it might be harder. Good luck with that.


----------



## devil_himself (Apr 7, 2007)

Will XxCopy .. Take Care Of The Leap Year ??


----------



## Squashman (Apr 4, 2003)

devil_himself said:


> Will XxCopy .. Take Care Of The Leap Year ??


No clue but I bet you could test it in a few days.


----------



## elroth (Feb 13, 2008)

Thankd devil_himself for the script.i will test the scripts. In the mean time, any idea how the xxcopy works.can you provide me the script using xxcopy.


----------



## Squashman (Apr 4, 2003)

elroth said:


> Thankd devil_himself for the script.i will test the scripts. In the mean time, any idea how the xxcopy works.can you provide me the script using xxcopy.


If you read the docuementation for xxcopy it will be quite revealing. They also have some great documentation on their website. But I think your best bet would be to use the FORFILES command from the 2003 resource kit.

I believe the xxcopy command would look something like this.
xxcopy c:\mydir\*.* /RS /S /H /DB#7
Delete all files from the source and subdirectories that are over 7 days old.

Take a look at this to use FORFILES
http://www.windowsitpro.com/Articles/ArticleID/44891/44891.html?Ad=1


----------



## ChuckE (Aug 30, 2004)

Leap Year? Why would you have any doubts about that? It is just one more day (Feb 29), nothing so earth shattering.

If you set up a command parameter for a date like: (copied from the xxcopy.com website)

```
The file date/time related XXCOPY switches.

The ten basic filetime switches fall into either of the two groups:
Comparison to the reference file (newer/older/same/different)

        /DA,  /DB,  /DS,  /DX         ; newer, older, same, different

    Relative and Absolute date specifiers (you give the range of date)

        /DA#n,    /DB#n,    /DO#n     ; as how many days ago from today
        /DA:date, /DB:date, /DO:date  ; date specified as yyyy-mm-dd
```
If you use any February day, it is going to work. Leap year is not going to have an impact.


----------



## elroth (Feb 13, 2008)

hi ,
can the same application be done with windows application in C#.Any body got any idea...?


----------



## devil_himself (Apr 7, 2007)

Search Here - http://www.codeproject.com

Have A Look
http://www.codeproject.com/KB/files/FileSystemEnumerator.aspx


----------

