Jump to content

Request : Script to Delete All But 10 Newest Files in Folder


Recommended Posts

Posted

I consider myself pretty competent in batch file scripting, but this one has me stumped.

 

We have roaming profile users (Argh) for whom the Recent folder of links is totally out of control. Where it should have the last few files they opened, it has 200+ shortcuts in.

 

Can anyone think of a programatical way to nuke all but the last 10 (for example) shortcuts in the folder, based on age? I can't nuke the whole folder and allow it to recreate itself as many of the users rely on "Recent Documents" rather than actually knowing where the hell they saved their work and I will have weeks of "My work is all gone!" :roll:

Posted

try this (again, needs cygwin or linux):

rm $(ls -Ac | tail -n $(echo $(($(ls -A| wc -l)-10))))

 

test it first and make sure you have backups.

Posted

The *nix solutions are all quite elegant but if you can't install Cygwin or Interix/SfU on your server then use FOR /F

 

 

FOR /F "skip=10 delims=?"  %a in ('DIR /o-d /b *.lnk') do DEL "%a"

 

The delims=? is for file names containing white spaces.

 

Oh yeah this assumes that the current directory is set to Recent Files of the relevant profile.

 

Test it with @ECHO rather than DEL to see if it's picking up the correct files then use DEL when ready.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...