Solved

How to sort and delete OLD log files in windows

Posted on 2013-05-13
7
387 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 53

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 53

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

This article covers how to install the Microsoft Windows Operating System (OS). What is covered in this article:  > Different Versions and Editions of the Windows OS  > Upgrading versus Fresh Installation of the OS           - Steps to take pr…
Ever notice how you can't use a new drive in Windows without having Windows assigning a Disk Signature?  Ever have a signature collision problem (especially with Virtual Machines?)  This article is intended to help you understand what's going on and…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …

809 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