Powershell Script to count .txt files only in a directory

我们两清 提交于 2019-12-02 09:07:15

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}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!