Solved

How to sort and delete OLD log files in windows

Posted on 2013-05-13
7
373 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
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
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
Comment Utility
Both scripts are not working for some reason
0
 
LVL 51

Expert Comment

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

~bp
0
 
LVL 13

Accepted Solution

by:
frankhelk earned 145 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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…

743 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

16 Experts available now in Live!

Get 1:1 Help Now