Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

Hey PowerShell Guy !,How Can I Delete All the Files in a Folder Except for the Most-Recently Created File?

Another Hit-in-one(liner) translation from a ca 30 lines VbScript example :

How Can I Delete All the Files in a Folder Except for the Most-Recently Created File?

del *.* -Exclude (dir | sort creationtime -desc)[0] -whatif

Remove the -WhatIf to actualy delete the files. 

Enjoy,

Greetings /\/\o\/\/

Published Thursday, May 15, 2008 5:38 PM by MoW
Filed under: ,

Comments

# re: Hey PowerShell Guy !,How Can I Delete All the Files in a Folder Except for the Most-Recently Created File?

I came up with this one-liner for deleting empty folders in a tree.  It works but tends to be slow.  I'm not sure it can be sped up.

Delete all folders (hidden and read-only included) if they are empty:

foreach ($dir in @(gci . -r -fo | where {$_.PSIsContainer} | sort -descending fullName)) {if (!(gci $dir.fullName -fo)) {ri $dir.fullName -fo}}

I figured since it was a similar file management task this might be interesting.  It's extremely useful if you use SD or P4.

Wednesday, June 18, 2008 10:29 PM by Chinpokomon
Anonymous comments are disabled