我的英語可能不完美,但我會盡力而為。
我正在嘗試撰寫一個 Powershell 腳本,其中檔案名末尾有一個數字,它應該經常列印。這有可能嗎?使用腳本它只列印 1 次。不管什么原因。。
param (
[string]$file = "C:\Scans\temp\*.pdf",
[int]$number_of_copies = 1
)
foreach ($onefile in (Get-ChildItem $file -File)) {
$onefile -match '\d$' | Out-Null
for ($i = 1; $i -le [int]$number_of_copies; $i ) {
cmd /C "lpr -S 10.39.33.204 -P optimidoc ""$($onefile.FullName)"""
}
}
uj5u.com熱心網友回復:
$number_of_copies當列印的次數取自檔案的 BaseName 時,不需要引數。
我會將您的代碼更改為:
param (
[string]$path = 'C:\Scans\temp'
)
Get-ChildItem -Path $path -Filter '*.pdf' -File |
# filter only files that end with a number and capture that number in $matches[1]
Where-Object { $_.BaseName -match '(\d )$' } |
# loop through the files and print
ForEach-Object {
for ($i = 1; $i -le [int]$matches[1]; $i ) {
cmd /C "lpr -S 10.39.33.204 -P optimidoc ""$($_.FullName)"""
}
}
在 內部ForEach-Object,在每次迭代中,$_自動變數代表當前的 FileInfo 物件。
PS您的腳本只列印每個檔案一次,因為您將引數$number_of_copies設定1為默認值,但代碼永遠不會將其更改為檔案名中找到的數字。
順便提一句。你的英語沒有問題
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/414567.html
標籤:
