1.

Solve : How do I create a batch file to delete log files older than 2 weeks?????

Answer»

Can anyone help me create a batch file that will delete log files in a certain directory that are older than 2 WEEKS?

Thanks!!!!! hefe, were not here to just make stuff for people. we are here to help people make stuff.

if have your "blueprints" or the code you are trying to make it out of, we can help you with thatBatchFileBasics:

hefe posted:
Quote from: hefe on July 22, 2009, 09:08:38 AM

Can anyone help me create a batch file

You said:
Quote from: BatchFileBasics on July 22, 2009, 11:13:34 AM
we are here to help people make stuff.


Isn't that what hefe asked for?Quote from: manadude2 on July 22, 2009, 11:32:10 AM
BatchFileBasics:

hefe posted:
You said:

Isn't that what hefe asked for?

but where is his progress so far?Quote from: manadude2 link=topic=88144.msg591102#msg591102
Isn't that what hefe asked for?
[/quote
you can ASK for help, but sometimes people want to see effort before helping.
Have a nice day. Have a nice day. Hi guys, it's not a batch file, but I've found a program that will delete files in a specific FOLDER that are older than the specified time.

It's called Cyber-D's Autodelete 2.15 and can be downloaded from here, here or here.I've seen this question (or one like it) a zillion times on this and other DOS sites, but never a pure DOS solution... thought it was about time I contributed. It looks a lot more complicated than it really is, but any comments or suggestions are welcome.


First the basics:
Code: [Select]@ECHO OFF
CLS

VERIFY OTHER 2>NUL
SETLOCAL ENABLEEXTENSIONS
IF ERRORLEVEL 1 (
ECHO Cannot enable Command Extensions
GOTO EndError
)

VERIFY OTHER 2>NUL
SETLOCAL ENABLEDELAYEDEXPANSION
IF ERRORLEVEL 1 (
ECHO Cannot enable Delayed Environment Variable Expansion
GOTO EndError
)
Which directory, and how many days of stuff do you want to keep? These are the only two variables you need to set.
Code: [Select]SET RetentionDays=14
SET TargetDirectory=C:\Temp
Obviously, you could change this variable usage to input parameters instead, to make this script more flexible. Also, consider a third argument for the filename.extension mask you want to keep/move/delete/whatever with this script. In this example, I am including ALL files.

Next, set a group of date variables to today:
Code: [Select]FOR /F "tokens=2-4 delims=/ " %%d IN ('ECHO %DATE%') DO (
SET dMonth=%%d
SET DDAY=%%e
SET dYear=%%f
)
The XCOPY command we are eventually going to use requires the format mm-dd-yyyy. Set the "Retention Date" and start with today:
Code: [Select]SET RetentionDate=%dMonth%-%dDay%-%dYear%
Main DATE logic -- see internal REMarks:
Code: [Select]IF %RetentionDays% GTR 0 (
REM --------------------------------------
REM Set the working date, start with today
REM --------------------------------------
SET fooMonth=%dMonth%
SET fooDay=%dDay%
SET fooYear=%dYear%

REM -----------------------------------------------------------------
REM Loop N times, where N=%RetentionDays%,
REM and subtract one from the Working Date.
REM
REM The called PreviousDay.cmd script is attached as a TXT file. I
REM adapted it from another script I found somewhere on the
REM interwebs. Credit to the original author, whoever s/he is.
REM
REM PreviousDay takes one input argument in the format mm dd yyyy
REM and echoes the previous day in the same format
REM -----------------------------------------------------------------
FOR /L %%l IN (1,1,%RetentionDays%) DO (
FOR /F "tokens=1-3" %%p IN ('CALL %COMSPEC% /E:ON /V:ON /C PreviousDay.cmd !fooMonth! !fooDay! !fooYear!') DO (
SET fooMonth=%%p
SET fooDay=%%q
SET fooYear=%%r
)
)

REM ---------------------------------------------------------------
REM Now set the Retention Date to the Working Date we've calculated
REM ---------------------------------------------------------------
SET RetentionDate=!fooMonth!-!fooDay!-!fooYear!
)

Main CLEANUP Logic -- see internal REMarks:
Code: [Select]REM -----------------------------------------------------
REM First, build a List of ALL files in %TargetDirectory%
REM -----------------------------------------------------
FOR /F "tokens=1 delims=|" %%b IN ('DIR /B %TargetDirectory%') DO (
REM optionally use FOR /F "tokens=1 delims=|" %%b IN ('DIR /B %TargetDirectory%\*.foo') DO (
REM -------------------------------
REM Store each filename in the list
REM -------------------------------
SET ThisFileName=%%b
SET ThisFile=%TargetDirectory%\!ThisFileName!
SET DeleteThisFile=TRUE

REM ----------------------------------------------------------------------
REM Next, build a list of files where datestamp >= (TODAY - RetentionDays)
REM ----------------------------------------------------------------------
FOR /F "tokens=*" %%k IN ('XCOPY %TargetDirectory%\*.* %TEMP% /L /D:!RetentionDate!') DO (
SET KeepFile=%%k

REM --------------------------------------------------
REM Exclude the "n File(s)" echo by testing with EXIST
REM --------------------------------------------------
IF EXIST !ThisFile! (
REM --------------------------------------------------------
REM If ThisFile = KeepFile then KeepFile will NOT be deleted
REM --------------------------------------------------------
IF /I "!ThisFile!"=="!KeepFile!" (
SET DeleteThisFile=FALSE
)
)
)

REM ---------------------------------------------------------------------------------------
REM Finally, test KeepThisFile and DELETE ThisFile if it was not found in the KeepFile list
REM ---------------------------------------------------------------------------------------
IF /I !DeleteThisFile!==TRUE (
ECHO DELETING !ThisFile!
DEL /F /Q !ThisFile! > NUL
)
)
That's it! Please note that this example deletes all files older than 14 days. In other words, it will keep all files date-stamped within 14 days, inclusive. Example: Today is 07/25/2009. That date minus 14 = 07/11/2009. Any files date-stamped 07/11/2009 (or 07/12, or 07/13, etc.) are not touched. Any files date-stamped 07/10 (or 07/09, or 07/08, etc.) are deleted.

Also, this does NOT perform \Folder\ deletion, and I haven't gone out of my way to handle path\filenames with embedded spaces, but it won't take much to incorporate either of those features.

Hope it helps!


[attachment deleted by admin]excellent solution, if I say so myself! Thanks! Every line makes me like my sig more n more. Quote from: hefe on July 22, 2009, 09:08:38 AM
Can anyone help me create a batch file that will delete log files in a certain directory that are older than 2 weeks?

Thanks!!!!!
you can see here (Example 2) for an example using vbscriptSee also http://home.mnet-online.de/horst.muc/wbat32.htm

DelAge32 deletes or moves files with a mimimum age given by the number of days, © 2003-2008, Horst SchaefferQuote from: gh0std0g74 on July 25, 2009, 10:48:20 PM
you can see here (Example 2) for an example using vbscript


Ghost,
The Batch solution is much better and they are on the right Board.Quote from: billrich on October 22, 2009, 11:51:16 AM

Ghost,
The Batch solution is much better and they are on the right Board.
date manipulation is always not better using cmd unless you have tools like GNU date , or GNU find or even vbscript , period.
GNU find, just one line does the job
Code: [Select]c:\test> gnu_find.exe c:\path -type f -mtime +14 -delete # delete files older than 14 days
vbscript does date OPERATION better than batch (cmd)...period.
Code: [Select]Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\temp")
For Each efile in objFolder.Files
If DateDiff("d",eFile.DateLastModified,Now) >= 30 Then
WScript.Echo "file found that is 1 month old: " & efile
WScript.Echo eFile.DateLastModified
objFSO.DeleteFile(eFile)
End If
Next

in the real world, one do not have to waste time doing unnecessary things when there are tools already there to help make one's job easier.

Lastly, why do you reply after 3 months...? You are so bored because nobody is nitpicking with you?


Discussion

No Comment Found