Link to home
Start Free TrialLog in
Avatar of LuiLui77
LuiLui77

asked on

Script to delete files from a particular directory

I am looking to automate the deletion of files on a particular directory. These files are created daily and I will need to delete all files except for the last 7 (last 7 days).

Does anyone has an idea on how to accomplish this?
ASKER CERTIFIED SOLUTION
Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bill Prew
Bill Prew

Here's a relatively simple BAT script approach. Sorts the files newest to oldest, and skips the 7 most recent and deletes the rest.

@echo off
for /f "tokens=* skip=7" %%A in ('dir /b /o-d /a-d "c:\temp\*.*"') do del "%%~A"

Open in new window

~bp
Avatar of LuiLui77

ASKER

Hey Dan, thank you for the command line it worked perfectly.

Since I will use this command line on a 2003 server, I had to install Powershell 2.0 first.