Solved

How to sort and delete OLD log files in windows

Posted on 2013-05-13
7
385 Views
Last Modified: 2014-06-11
Have a folder called logs  

which has file since last 30 days i want to delete all the .Logs sorted by timestamp  and delete previous 15 days of logs file  

i want to make a Batch file  and run the cron every  1 month to delete the 15 days logs

Can any one help

Sorting dir /OD   works  and no idea how to do the next delete
0
Comment
Question by:reddy2388
7 Comments
 
LVL 5

Expert Comment

by:Eddie-Lopez
ID: 39162741
Hi,

You can make a batch and run it with Scheduled Tasks.

See here:

http://www.howtogeek.com/50528/automating-the-process-of-deleting-old-log-files/
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 39162753
Okay, so the basic idea is you use the DIR command with /O-D to sort the files in the folder in reverse date order (newest first).  Then you process that sorted list and skip over the first 15 files (or however many you want to keep) and then delete the rest.  This is pretty easy to do in a small BAT file, that can then be scheduled with Windows Scheduler.

@echo off
setlocal

set BaseDir=c:\temp\logs
for /F "tokens=* skip=15" %%A in ('dir /b /a-d /o-d "%BaseDir%\*.*"') do (
  del "%BaseDir%\%%~A"
)

Open in new window

~bp
0
 

Author Comment

by:reddy2388
ID: 39201337
Both scripts are not working for some reason
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 39201542
Can you share any additional info?  Are you getting an error of some sort, etc?

~bp
0
 
LVL 14

Accepted Solution

by:
frankhelk earned 145 total points
ID: 39246863
I have a much better solution to offer.

Try the command FORFILES within a batch file, called cyclic with AT or scheduled tasks.

I.e.
forfiles /P D:\logfiles /D -4 /M *.log /C "cmd /c erase /q @path"

Open in new window

would look in D:\logfiles for all files matching "*.log" who are older than 4 days and delete them.

More options available.

It's a transient command, which means it's a separate .exe. If not available im your Windows version (should be available in anything newer than XP/2003 server), you might simply copy it from a machine that has it. Seems to be part of some Windows ressource kits, and may be found for download in many place on the net - just google for FORFILES.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The way I use Experts Exchange to assist me in analyzing and diagnosing a problem is I first enter a Verbose Question at Experts Exchange like: Office 2007 will hang when opening and saving files I then launch WordPad (any text editor will do) an…
This is a little timesaver I have been using for setting up Microsoft Small Business Server (SBS) in the simplest possible way. It may not be appropriate for every customer. However, when you get a situation where the person who owns the server is i…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now