6

Challenge for you guys, here:

I need some method (preferably built-in, but F/OSS third-party apps may also be considered) to generate a list of sub-folders, at a certain level, along with (or, better, filtered by) their "modified date". Please note, that I need a list of folders only - the output should exclude files.

Example:

For folder structure:

C:
\---Reports
    +----Task1
    |    \----Report1
    |         +----CSV
    |         +----HTML
    |         \----XML
    +----Task2
    |    +----Report1
    |    |    +----CSV
    |    |    +----HTML
    |    |    \----XML
    |    +----Report2
    |    |    +----CSV
    |    |    +----HTML
    |    |    \----XML
    |    \----Report3
    |         +----CSV
    |         +----HTML
    |         \----XML
    \----Task3
         +----Report1
         |    +----CSV
         |    +----HTML
         |    \----XML
         \----Report2
              +----CSV
              +----HTML
              \----XML

I want the list to have information similar to:

PATH                         | MODIFIED
=============================|==========
C:\Reports\Task1\Report1     | 6/23/2011
C:\Reports\Task2\Report1     | 6/17/2011
C:\Reports\Task2\Report2     | 6/30/2011
C:\Reports\Task2\Report3     | 7/06/2011
C:\Reports\Task3\Report1     | 6/22/2011
C:\Reports\Task3\Report2     | 7/13/2011

(Note the exclusion of folders above or below the level of "Report#" folders.)

Or, ideally, I'd like something that could generate a report like this:

PATH                         | MODIFIED
=============================|==========
C:\Reports\Task1\Report1     | 6/23/2011
C:\Reports\Task2\Report1     | 6/17/2011
C:\Reports\Task2\Report2     | 6/30/2011
C:\Reports\Task3\Report1     | 6/22/2011
=============================|==========
TOTAL                        |         4

(Note that this represents a filtered version of the previous list, restricted to a certain date range.)

This is going to be done in Windows XP/2003. PowerShell is available. Again, I'd rather use available built-in utilities, but F/OSS tools may be considered also.

1 Answer 1

9

Powershell:

gci C:\Reports\*\* | where {$_.LastWriteTime -gt ((get-date)- (new-timespan -day 7))} | select FullName,LastWriteTime | ft

Breaking it down a bit:

gci (aka get-childitem) does what it sounds like, treats the supplied file system path as an object and pulls the child items (files or folders) from it.

That gets piped to the where statement, which has a single filter function as it's argument.

The filter function is a 'simple' comparison, in this case {A -gt B} = "True if A is Great Than B". The first part of this is $_.LastWriteTime which takes each object passed from the pipe and pulls the LastWriteTime property out. The last part of this is a relative date construct; nothing is super simple in PS. The first part of date thing gets the current date, then it subtracts an amount of time from that number (in this example I'm creating a new date object that contains the value of 7 days; so now minus 7 days would give the date 7 days ago).

From those objects that made it through the where filtering I'm pulling out just the FullName and LastWriteTime properties, since that's all we care about.

Finall it's piped to the format-table (aka ft) command so it makes a pretty table like this:

FullName                           LastWriteTime
--------                           -------------
C:\Reports\Task1\Report1           7/11/2011 10:00:00 AM
C:\Reports\Task2\Report1           7/10/2011 5:00:00 AM
C:\Reports\Task2\Report2           7/6/2011 9:00:00 PM
C:\Reports\Task4\Report7           7/4/2011 3:00:00 PM

To get just the count of all that, enclose the above command like this: (mess_from_above_less_the_ft_part).count (Have to take the "| ft" part off or it adds 4 lines of table formatting that get counted and will throw your count off by 4.

8
  • 1
    Wow. I've got a lot of PowerShell learning to do. Though most of it seems fairly logical, do you mind breaking it down a bit more? Also, a copy of the output would be cool to have here, too.
    – Iszi
    Commented Jul 11, 2011 at 19:20
  • Okay, I've run the command on my target, and the output is a bit too verbose to be immediately useful. Is there any way to pare that down a bit, so that there's only one line per item found? Also (or alternatively) can I get PowerShell to just give me a total of the search results?
    – Iszi
    Commented Jul 11, 2011 at 19:35
  • @Iszi, changed it a bit to be prettier, and option for just the count.
    – Chris S
    Commented Jul 11, 2011 at 19:46
  • Ack! I'm missing one more step (will add to OP also) - need to retrieve directories only. Thanks again, for the great help!
    – Iszi
    Commented Jul 11, 2011 at 20:18
  • 1
    You can add it to the current where statement so that the where part of the pipeline becomes: where {($_.LastWriteTime -gt ((get-date)- (new-timespan -day 7))) -and ($_.psIsContainer -eq $True)} Commented Jul 12, 2011 at 3:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .