Powershell 进度条 [英] Powershell Progress Bar
本文介绍了Powershell 进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 Powershell 的新手,并且在使用 foreach-object 循环的进度条时遇到问题(如果可能的话)
I'm new to Powershell and am having a problem getting a progress bar to work with a foreach-object loop (If it is even possible)
感谢下面的 Chris,这是我目前所拥有的,我的问题是进度条到达某个点,然后出现错误:101 参数大于允许的最大范围 100:
Thanks to Chris below is what I have so far, my problem here is that the progress bar gets to a point and then I get error: The 101 argument is greater than the maximum allowed range of 100:
$FolderList = Get-Content C:\Folders.txt
$i = 0
foreach( $Folder in $FolderList )
{
Write-Host $Folder
Get-ChildItem $Folder -Recurse *.pdf | foreach-object{
$fileCount = (Get-ChildItem $Folder).Count
$i += 1
Write-Progress -Activity "Counting Files" -status "Searching...." -percentComplete (($i / $fileCount)*100)
$pdf = c:\pdftk.exe $_.FullName dump_data
$NumberOfPages = [regex]::match($pdf,'NumberOfPages: (\d+)').Groups[1].Value
New-Object PSObject -Property @{
Name = $_.Name
FullName = $_.FullName
NumberOfPages = $NumberOfPages
}
}
}
推荐答案
这是我解决问题的方法:
Here's my approach to the problem:
$i = 0
$pdfFiles = @()
#First, get the files and add them to a collection:
foreach ($folder in $FolderList){
Get-ChildItem $Folder -Recurse *.pdf | %{$pdfFiles += $_}
}
#Measure the collection
$fileCount = ($pdfFiles | Measure-Object).Count
#Do work on the collection
$pdfFiles | foreach-object{
$pdf = c:\pdftk.exe $_.FullName dump_data
$NumberOfPages = [regex]::match($pdf,'NumberOfPages: (\d+)').Groups[1].Value
New-Object PSObject -Property @{
Name = $_.Name
FullName = $_.FullName
NumberOfPages = $NumberOfPages
}
$i += 1
Write-Progress -Activity "Counting Files" -status "Searching...." -percentComplete (($i / $fileCount)*100)
}
这篇关于Powershell 进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文