最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

powershell - I need to get a count of directories or files that have their archive bit set PS7.5 - Stack Overflow

programmeradmin3浏览0评论

My initial goal was to get a list of the directories and files that had their archive bit set but then I realized that took some time to accomplish and I really wanted to know if there were any dirs or files on the drive that needed backed up. I have a lot of static drives that get updated occasionally but don't need backed up on a regular basis. My backup software is sector based so not a lot of savings by doing differentials in fact they can be a lot slower.

Get-ChildItem -Path (-join($Drive, ":\*.*")) -recurse -force 
    | where {(Get-ItemProperty $_.fullname).attributes -BAND [io.fileattributes]::archive}

I saw another post from older PowerShell versions that suggested that I needed to include a pipe for | measure ).Count, but I don't know how to incorporate it into what I have already. That value then has to be used in an if statement to skip or continue the rest of the process but it doesn't make sense to recode all that adding it into the if. In my head that makes it more complicated but I'm not a PowerShell expert by any means.

Based on some of the remarks below I reworked the PS from the legnthy initial one above to this:

Get-ChildItem -Path C:\ -Recurse -Force -Attributes Archive

but I still don't understand where the .count goes. I tried wrapping this in ( ) and adding the .count on the end that didn't work. I also tried adding | (Measure-Object).count past the main cmdlet closing ) that didn't work and I also tried putting it in the main cmdlet ( ).

I also realize that I only need to know if there are any or none so once it exceeds 0 I want to stop but the way PS works it returns an array rather than each item in a sequential process almost like SQL would return a dataset.

My initial goal was to get a list of the directories and files that had their archive bit set but then I realized that took some time to accomplish and I really wanted to know if there were any dirs or files on the drive that needed backed up. I have a lot of static drives that get updated occasionally but don't need backed up on a regular basis. My backup software is sector based so not a lot of savings by doing differentials in fact they can be a lot slower.

Get-ChildItem -Path (-join($Drive, ":\*.*")) -recurse -force 
    | where {(Get-ItemProperty $_.fullname).attributes -BAND [io.fileattributes]::archive}

I saw another post from older PowerShell versions that suggested that I needed to include a pipe for | measure ).Count, but I don't know how to incorporate it into what I have already. That value then has to be used in an if statement to skip or continue the rest of the process but it doesn't make sense to recode all that adding it into the if. In my head that makes it more complicated but I'm not a PowerShell expert by any means.

Based on some of the remarks below I reworked the PS from the legnthy initial one above to this:

Get-ChildItem -Path C:\ -Recurse -Force -Attributes Archive

but I still don't understand where the .count goes. I tried wrapping this in ( ) and adding the .count on the end that didn't work. I also tried adding | (Measure-Object).count past the main cmdlet closing ) that didn't work and I also tried putting it in the main cmdlet ( ).

I also realize that I only need to know if there are any or none so once it exceeds 0 I want to stop but the way PS works it returns an array rather than each item in a sequential process almost like SQL would return a dataset.

Share Improve this question edited 2 days ago CityguyUSA asked Mar 31 at 2:58 CityguyUSACityguyUSA 11 bronze badge 2
  • You might just do: (Get-ChildItem...archive).count – iRon Commented Mar 31 at 6:04
  • I'm not exactly sure where you are suggesting doing this? The first part of the cmdlet is the name of the cmdlet it is not in parens the second part tells it what data to examine, the third part following the | is limiting the data by atributes. I need to count of the resulting dataset. – CityguyUSA Commented 2 days ago
Add a comment  | 

2 Answers 2

Reset to default 2

You can use Get-ChildItem -Attributes to filter based on file system attributes:

$archiveDirectories = Get-ChildItem -Attributes Archive+Directory -Recurse

To use Measure-Object, simply tack it onto the end of the pipeline expression:

$archiveDirectoryCount = (Get-ChildItem -Attributes Archive+Directory -Recurse |Measure-Object).Count

You can combine parameters -Directory and -Attributes, with PowerShell 7.5:

$ArchiveDirectories = @()
$RootFolder = 'C:\', 'D:\'
$RootFolder |
    ForEach-Object -Process {
        # The -Force parameter also filters hidden and system
        $ArchiveDirectories += Get-ChildItem -LiteralPath $PSItem -Attributes 'Archive' -Directory -Recurse -Force
    }
$AmountArchiveDirectories = $ArchiveDirectories.Count

Regarding the important comment from mklement0, I made the necessary reservation, as the number of iterations is very small, but I add a solution that does not use the += operator.

$ArchiveDirectories = @{}
$RootFolder = 'C:\', 'D:\'
$RootFolder |
    ForEach-Object -Process {
        # The -Force parameter also filters hidden and system
        $ArchiveDirectories[ $PSItem ] =
            Get-ChildItem -LiteralPath $PSItem -Attributes 'Archive' -Directory -Recurse -Force
    }
$AmountArchiveDirectories =
    $ArchiveDirectories.GetEnumerator() |
        ForEach-Object -Process {
            $PSItem.Value.Count
        } |
        Measure-Object -Sum |
        Select-Object -ExpandProperty 'Sum'
发布评论

评论列表(0)

  1. 暂无评论