Solved

How to sort and delete OLD log files in windows

Posted on 2013-05-13
7
400 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 55

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 55

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Have you ever had a hard drive that you can't boot into, but need to change the registry? Here is the solution! This article guides you through accessing and editing a registry of a non-primary drive. To read registry information on a non-prim…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
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). …
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…

724 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