+ Post New Thread
Results 1 to 4 of 4
Scripts Thread, Request : Script to Delete All But 10 Newest Files in Folder in Coding and Web Development; I consider myself pretty competent in batch file scripting, but this one has me stumped. We have roaming profile users ...
  1. #1

    Join Date
    Feb 2006
    Location
    Derbyshire
    Posts
    1,381
    Thank Post
    181
    Thanked 211 Times in 171 Posts
    Rep Power
    66

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

    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!"

  2. #2


    Join Date
    Jan 2006
    Posts
    8,202
    Thank Post
    442
    Thanked 1,033 Times in 813 Posts
    Rep Power
    341

    Re: Request : Script to Delete All But 10 Newest Files in Fo

    Code:
     find . -mtime +10 -type f -exec rm -rf {} \;
    will delete files modified more than 10 days ago - requires cygwin or linux

  3. #3

    Join Date
    May 2006
    Posts
    66
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

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

    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.

  4. #4

    Join Date
    Feb 2006
    Posts
    1,187
    Thank Post
    0
    Thanked 1 Time in 1 Post
    Rep Power
    0

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

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


    Code:
    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.



SHARE:
+ Post New Thread

Similar Threads

  1. Delete a folder using scripting
    By FN-GM in forum Scripts
    Replies: 15
    Last Post: 22nd December 2007, 05:44 PM
  2. Replies: 8
    Last Post: 17th November 2007, 10:04 PM
  3. Got a folder I cannot delete or take ownership of
    By sidewinder in forum Windows
    Replies: 2
    Last Post: 20th March 2007, 03:49 PM
  4. Replies: 1
    Last Post: 21st January 2007, 03:51 PM
  5. Replies: 5
    Last Post: 5th March 2006, 02:22 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •