Powershell Script to count .txt files only in a directory

前端 未结 1 450
北海茫月
北海茫月 2021-01-25 23:43

My goal is to send email to some users to notify about the current count of the files inside some folder directories.

I need to count files only with .

1条回答
  •  抹茶落季
    2021-01-26 00:19

    I found the following workaround

    1. by using -filter instead of -include

    just remove the -recurse to exclude subdirectories files in counting and use -filter instead of -include

    $path = "D:\LIVE"
    
     $total = Get-ChildItem $path  -File -filter *.txt |Where-Object {$_.LastWriteTime} | Measure-Object | ForEach-Object{$_.Count}
    
    1. using get-Item instead of Get-ChildItem with -include

    -Filter is limited to using one paramter, so if you want to use -include to use as many searching parameter as you can, use Get-Item instead of get-childItem

    just add * (when no path is declared) or * appending to the existing path

    $path = "D:\LIVE\*"
    
     $total = Get-Item $path -include *.txt, *.xml  |Where-Object {$_.LastWriteTime} | Measure-Object | ForEach-Object{$_.Count}
    

    0 讨论(0)
提交回复
热议问题