Finding unused/dormant storage folders

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

Finding unused/dormant storage folders

Post by MigrationUser »

23 Jul 2010 14:54
Simon Sheppard

Here is a script to print the newest file in each of a series of top level folders.

This is similar to what the 'diruse' or du utilities do but looking at age rather than filesize, so is useful for finding dormant folders that are not being used/written to.

Code: Select all

# List all top level folders (not files)
dir "\\fileserver01\Workgroups" | ?{$_.PsIsContainer -eq $true} |
foreach {
   # For each folder look through all the files (not folders) and display the most recent file.
   dir $_.fullname -recurse | ?{$_.PsIsContainer -ne $true}| sort -prop LastWriteTime | select -last 1
 }
The script above is not particularly fast as it will touch every file in the hierarchy, and has to do some fairly big sorts.

----------------------------

#20 Nov 2010 08:51
jumper
Simon Sheppard wrote:

The script above is not particularly fast as it will touch every file in the hierarchy, and has to do some fairly big sorts.

Code: Select all

PS  ~walid2mi> 
                  (measure-command {
                  dir . | ?{$_.PsIsContainer -eq $true} |
                  foreach {
                   # For each folder look through all the files (not folders) and display the most recent file.
                  dir $_.fullname -recurse | ?{$_.PsIsContainer -ne $true}| sort -prop LastWriteTime |select -last 1}
                  }).Milliseconds

>>> 131
PS  ~walid2mi> 
                  (measure-command {
                  foreach($item in (dir . | ? {$_.PSIsContainer})) {
                  dir $item.fullname -rec | ?{!$_.PsIsContainer}| sort LastWriteTime | select -last 1}
                 }).Milliseconds

>>> 126 
PS  ~walid2mi> 
Simon Sheppard wrote:

| sort -prop LastWriteTime | select -last 1
another test:

Code: Select all

PS  ~walid2mi> 
                (measure-command {ps|sort id|select id}).MilliSeconds

>>> 15

PS  ~walid2mi> 
                (measure-command {ps|select id|sort id}).MilliSeconds

>>> 11
----------------------------

# 20 Nov 2010 13:28
Simon Sheppard


Interesting, I can see how doing the select before a sort would speed things up, but I'm surprised the 'foreach($item' in your first example is that much faster than a pipe.

----------------------------

#10 Jan 2011 01:44
saintly


Hi,
i'm a Powershell novice so forgive me if this is a basic question
I have worked out how to get the results of the code above to output into a file like this
foreach($item in (dir . | ? {$_.PSIsContainer})) {dir $item.fullname -rec | ?{!$_.PsIsContainer}| sort LastWriteTime | select -last 1 >>c:\temp\output.txt}
and it seems to work fine.
Is it possible for me to now modify the script to only list the files/folders that the latest file is older than a specified date or number of days.
What i'm looking to do is to list any folders that have not had a file accessed in X number of days or since XX/XX/XXXX date (which ever is easier)

Thanks
Ian

----------------------------

#04 Feb 2011 13:55
Simon Sheppard


Take a look at this page
https://ss64.com/ps/syntax-delolder.html

You can just adapt that to list the files instead of deleting them
Post Reply