我想用带有函数的Powershell检查一个文件夹中有多少pdf



我想检查一个文件夹有多少pdf文件。例如,在我的C:\Temp\Test文件夹中,有6个pdf文件和6个txt文件。

PS H:> dir c:temptest

Directory: C:temptest

Mode                 LastWriteTime         Length Name                                                                                                                                                                         
----                 -------------         ------ ----                                                                                                                                                                         
-a----        21/01/2022  10:05 PM            611 1.pdf                                                                                                                                                                        
-a----        26/01/2022   5:22 PM              0 1.txt                                                                                                                                                                        
-a----        22/01/2022  12:19 PM           3939 2.pdf                                                                                                                                                                        
-a----        26/01/2022   5:22 PM              0 2.txt                                                                                                                                                                        
-a----        08/01/2022   4:53 PM          27992 3.pdf                                                                                                                                                                        
-a----        26/01/2022   5:22 PM              0 3.txt                                                                                                                                                                        
-a----        08/01/2022   4:53 PM          27992 4.pdf                                                                                                                                                                        
-a----        26/01/2022   5:22 PM              0 4.txt                                                                                                                                                                        
-a----        22/01/2022  12:19 PM           3939 5.pdf                                                                                                                                                                        
-a----        26/01/2022   5:22 PM              0 5.txt                                                                                                                                                                        
-a----        21/01/2022  10:05 PM            611 6.pdf   

如果我使用以下PS,我得到了正确的结果6

PS H:> $directoryInfo = Get-ChildItem C:TempTest | Where-Object {$_.Extension -eq ".pdf"}
$file_count = $directoryInfo.count
$file_count
6

然而,当我像函数一样使用它时,删除了文件夹中的2个pdf。它仍然显示为6个PDF。而函数中的$file_count显示为4。

PS H:> function CheckFileCount($Folder) {
$directoryInfo = Get-ChildItem $Folder | Where-Object {$_.Extension -eq ".pdf"}
$file_count = $directoryInfo.count
return $file_count
}
dir C:TempTest
CheckFileCount C:TempTest
Write-Host "There are $file_count PDFs in this folder."

Directory: C:TempTest

Mode                 LastWriteTime         Length Name                                                                                                                                                                         
----                 -------------         ------ ----                                                                                                                                                                         
-a----        26/01/2022   5:22 PM              0 1.txt                                                                                                                                                                        
-a----        26/01/2022   5:22 PM              0 2.txt                                                                                                                                                                        
-a----        08/01/2022   4:53 PM          27992 3.pdf                                                                                                                                                                        
-a----        26/01/2022   5:22 PM              0 3.txt                                                                                                                                                                        
-a----        08/01/2022   4:53 PM          27992 4.pdf                                                                                                                                                                        
-a----        26/01/2022   5:22 PM              0 4.txt                                                                                                                                                                        
-a----        22/01/2022  12:19 PM           3939 5.pdf                                                                                                                                                                        
-a----        26/01/2022   5:22 PM              0 5.txt                                                                                                                                                                        
-a----        21/01/2022  10:05 PM            611 6.pdf                                                                                                                                                                        
4
There are 6 PDFs in this folder.

看起来函数无法返回$file_count值。这里怎么了?提前谢谢。

谢谢大家,我想问题已经解决了。

当只调用函数时,您的函数对我来说是完美的。

我添加了一篇关于PowerShell中作用域的非常好的文章:PowerShellScopes


您的问题是:

写入主机"此文件夹中有$file_count PDF">

尝试将函数结果保存到类似这样的var中:

$function_result=CheckFileCount"C:\Software\example">

然后查看结果:

写入主机"此文件夹中有$function_result PDF">

最新更新