Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I use a PowerShell command to count ZIP files modified within the past 30-ish days, that goes something like this:

(gci "\\servername\sharename\foldername\subfoldername\*.zip" | where {$_.LastWriteTime -gt ((get-date)-(new-timespan -day 35))}).count

However, my true intention with this script is to get a count of files modified within the previous calendar month. Obviously, this script has a couple weaknesses.

  • Whenever I run the script, I have to calculate the days between now and the beginning of the previous month and then edit the script accordingly.
    • I recognize that I might have to edit the script every time anyway, but having to count days instead of just specifying a month is a little irksome.
  • The script will also catch any files modified within the current month.

Is there a way I can modify this command so that it only selects files modified within a specific month, or is this just a limitation of PowerShell?

If the script can be made to target a specific month, can it then also be written to auto-target the previous month so I don't have to modify it every time?


EDIT: Much thanks to zdan for the help here, and also to Google for walking me through some other issues. I figured I'd post my full, final script here in case anyone was interested.

This script counts ZIP files in the target directory which were modified within the previous month. The IF/ELSE statement early on helps account for cases where the current month is January. This script also depends on a file called Targets.txt, in the current working folder, in which one or multiple target systems may be listed (one machine name or IP per line). The script gracefully handles and logs errors if a target system or folder is unreachable, and all results are output to a text file (dated with the target month/year) in the current working folder. I've also made adjustments for compatibility with PowerShell versions before 3, where (gci).count would return nothing when the value should be zero or one. (Solved in this question.)

$ErrorActionPreference = 'Stop'
if ((get-date).Month -eq 1)
{
    $TargetMonth = 12
    $TargetYear = (get-date).Year-1
}
else
{
    $TargetMonth = (get-date).Month-1
    $TargetYear = (get-date).Year
}
echo "ZIP file counts for $TargetMonth/$TargetYear" | Out-File "$TargetMonth-$TargetYear ZIPs.txt"
echo "----------------------------------" | Out-File "$TargetMonth-$TargetYear ZIPs.txt" -Append
echo "`n" | Out-File "$TargetMonth-$TargetYear ZIPs.txt" -Append
foreach ($Target in gc 'Targets.txt')
{
    try
    {
        $ThisTarget = @(gci "\\$Target\c$\Folder\Subfolder\ZIPs\*.zip" | where {($_.LastWriteTime.Month -eq $TargetMonth) -and ($_.LastWriteTime.Year -eq $TargetYear)}).count
        $ZIPTotal += $ThisTarget
        echo "$Target has $ThisTarget ZIPs." | Out-File "$TargetMonth-$TargetYear ZIPs.txt" -Append
    }
    catch
    {
        echo "$Target could not be reached or ZIPs directory not found." | Out-File "$TargetMonth-$TargetYear ZIPs.txt" -Append
        $ZIPsErrors ++
    }
}
echo "----------------------------------" | Out-File "$TargetMonth-$TargetYear ZIPs.txt" -Append
echo "`n" | Out-File "$TargetMonth-$TargetYear ZIPs.txt" -Append
echo "The total number of ZIPs counted is $ZIPsTotal" | Out-File "$TargetMonth-$TargetYear ZIPs.txt" -Append
echo "Total number of errors encountered is $ZIPsErrors" | Out-File "$TargetMonth-$TargetYear ZIPs.txt" -Append
rv TargetMonth
rv TargetYear
rv Target
rv ThisTarget
rv ZIPsTotal
rv ZIPsErrors
$ErrorActionPreference = 'Continue'
share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

The beauty of powershell is that you get full-blown .NET objects that you can inspect. In the case of dates, powershell uses the DateTime class which exposes Month and Year properties that you can take advantage of:

(gci "\\servername\sharename\foldername\subfoldername\*.zip" | 
    ?{ $lwt = $_.LastWriteTime; ($lwt.Month -eq ((get-date).Month-1)) -and (($lwt.Year -eq (Get-Date).Year))}).count

Important: The above will not work if you run it in January. You'll need to adjust the year and month accordingly when that happens.

share|improve this answer
    
Is ? an alias for where that I'm forgetting? –  Iszi May 3 '13 at 20:05
    
@Iszi Yes it is. –  zdan May 3 '13 at 21:30
    
Reviewing this, I'm trying to simplify it just a bit. Is $lwt = $_.LastWriteTime; really necessary, or could I remove that and just insert $_.LastWriteTime in place of $lwt everywhere else? –  Iszi Aug 8 '13 at 18:19
    
@Iszi yes you can, it's just up to personal preference. –  zdan Aug 8 '13 at 18:22
    
This is strange. For instances where the count should be 1, I get a blank output. Other cases seem to work fine. Thoughts? –  Iszi Aug 8 '13 at 19:40
show 3 more comments

use a batch file would be much simple

DIR /TW | find /C "-12-"
share|improve this answer
1  
Doesn't this search for "dec" in the file name? I need to check the file's modified date - the time is not specified in the filename. –  Iszi May 3 '13 at 18:21
    
It's not searching for file name, its on modified date. –  Antony Lee Jul 29 '13 at 16:48
    
The original script (For /f %f in ('dir /b *.zip | find "dec"') DO set /a i=i+1) would have searched for any line in the DIR output that contained a string matching "dec". That search would not trigger on any date fields, since they are all numeric, and would likely trigger on a lot of potential false-positive cases where "dec" happens to be part of the file name. –  Iszi Jul 29 '13 at 17:16
    
Similarly, your new script (DIR /TW | find /C "-12-") only does a string search for "-12-" within the output of DIR. I don't know about other regions, but this doesn't match the date format in DIR output for the U.S. (MM/DD/YYYY) and again would trigger on false-positives where "-12-" just happens to be part of the file name. Even if I adapted it to my locale, it would trigger false-positives on any file modified on the 12th of any other month. –  Iszi Jul 29 '13 at 17:17
    
Obviously you are trying to drill down into power shell instead of trying to get things done in quick ways. Seriously i just have no trust to MS power shell syntax stability. Spend days to make it works then it will fade out once PS change version. PS, PS2, PS3 .... lol –  Antony Lee Jul 30 '13 at 13:33
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.