Solved

How to sort and delete OLD log files in windows

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article describes how to set permissions to allow a limited-permissions user to start and stop a particular System Service.   It is always best to give users only the permissions that they need to perform their job, so tweaking particular permi…
When you upgrade from Windows 8 to 8.1 or to Windows 10 or if you are like me you are on the Insider Program you may find yourself with many 450MB recovery partitions.  With a traditional disk that may not be a problem but with relatively smaller SS…
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…

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