我正在遍歷一個包含檔案的檔案夾,如果它們與我們當前的日期相匹配,則相應地選擇其中的一些。如果它沒有歸檔與當天匹配的任何檔案,我想輸出一條訊息,但我只輸出一條訊息而不是每個檔案的一條訊息。
關于如何在下面的代碼中實作這一點的任何線索:
foreach ($file in Get-ChildItem -Path $directory) {
if ($file.LastWriteTime.Date -eq [datetime]::Today) {
Write-Output "File from today"
} else {
Write-Output "File is not from today"
}
}
uj5u.com熱心網友回復:
您可以存盤是否在布爾變數中找到搜索結果。
$hasFile = $false
foreach ($file in Get-ChildItem -Path $directory)
if ($file.LastWriteTime.Date -eq [datetime]::Today) {
Write-Output "File from today"
$hasFile = $true
}
}
if ($hasFile -eq $false) {
Write-Output "No file found"
}
uj5u.com熱心網友回復:
我認為您可以通過這種方式進行處理,首先嘗試查找今天的任何檔案,如果沒有,則顯示訊息。
注意:我添加了-File僅查找檔案的開關,否則我們還會查看不打算查找的檔案夾。
$todayFiles = Get-ChildItem . -File | Where-Object {
$_.LastWriteTime.Date -eq [datetime]::Today
}
if ($todayFiles) {
$todayFiles | ForEach-Object { "File from Today => {0}" -f $_.FullName }
} else {
"File is not from today"
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/372232.html
標籤:电源外壳
