Monthly Archives: November 2009

How To Keep A Folder Clean From Old Files? #PowerShell

With PowerShell it is easy to cleanup out-of-date files in a temp folder, log folder, or whatever folder.

This Post shows two approaches:

– Delete All Files Older Than x Days
– Keep The x Youngest Files

How To Delete All Files Older Than x Days?

Let’s say you need to remove all files in %TEMP% and its subdirectories that weren’t modified during the past 10 days:

By piping the result of Get-ChildItem (dir) to Where-Object (?), all out-of-date files will be identified by comparing their LastWriteTime property with current date minus 10 days) and piped to Remove-Item (del)

How To Keep The x Youngest Files?

This time, you want to remove all files in %TEMP% except the 50 youngest files with PowerShell V2:

By sorting the result of Get-ChildItem (dir) by LastWriteTime in descending order and piping it to Select-Object (select) with setting its Skip parameter to 50, all but the 50 youngest files will be removed. Note that this requires Windows PowerShell V2 because V1’s Select-Object doesn’t have the Skip parameter!

With PowerShell V1, you could save the array of files returned by Sort-Object –Descending to a variable, and then process this array from skip count to total count as follows:

Hope this helps

Batch Scripting Howto – Where is it?

Category : Batch , Note

Initially I intended to migrate the stuff related to batch scripting to the new blog as well. Due to lack of time and motivation I decided to postpone this task or rather to wait if I receive requests like “I’m wondering if you are considering bringing back your old content back”

I will tell you a secret: the old site is still online – check Batch Howto. In order to avoid trouble with the new site I needed to remove the permalinks, therefore I’m afraid your bookmarks/favorites to old site content may be broken.