Solved

Script to delete files from a particular directory

Posted on 2014-02-13
3
226 Views
Last Modified: 2014-02-20
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?
0
Comment
Question by:LuiLui77
3 Comments
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 39857642
Try this:
gci -Path x:\your\path | %{ if($_.LastWriteTime -lt (Get-Date).AddDays(-7)) {$_.Delete()}}

Open in new window

HTH,
Dan
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 39857951
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
0
 

Author Closing Comment

by:LuiLui77
ID: 39874085
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Synchronize a new Active Directory domain with an existing Office 365 tenant
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

929 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

17 Experts available now in Live!

Get 1:1 Help Now