admin管理员组文章数量:1356889
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 |2 Answers
Reset to default 2You 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'
版权声明:本文标题:powershell - I need to get a count of directories or files that have their archive bit set PS7.5 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743969505a2570499.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
(Get-ChildItem...archive).count
– iRon Commented Mar 31 at 6:04