Powershell - 如果檔案(通配符)存在且大於 0kb

Powershell - 如果檔案(通配符)存在且大於 0kb

我可以使用以下命令成功檢查檔案是否存在且大於 0kb:

$getFILE = 'C:\DIR\FILE1.txt'

IF (test-path $getFILE){
    IF ((get-item $getFILE).length -gt 0) {
        "OK";return 0
    }
    ELSE {
        "0 bytes";return 2
    }
}
ELSE {
    "doesn't exist";return 1
}

我可以使用以下命令檢查通配符檔案是否存在:

$getFILE = 'C:\DIR\FILE*.*'

IF (test-path $getFILE){
    "OK";return 0
}
else {
    "doesn't exist";return 1
}

但我無法讓這兩個概念一起工作。下面,如果通配符檔案存在;傳回值始終為 0:

$getFILE = 'C:\DIR\FILE*.*'

IF (test-path $getFILE){
    IF ((get-item $getFILE).length -gt 0) {
        "OK";return 0
    }
    else {
        "0 bytes";return 2
    }
}
else {
    "doesn't exist";return 1
}

答案1

好吧,再多嘗試一下,我就想出了一些有效的方法:

$vDIR = 'C:\DIR'
$vFILE = 'FILE*.*'

$proc = @(Get-ChildItem $vDIR -Recurse -Include $vFile | Where {$_.length -gt 0})
If ($proc.count -gt 0) {
   ForEach ($item in $proc) {
      Do a bunch of stuff
   }
Else {
   Do this other thing
}

如果有人有更乾淨的解決方案,我仍然很樂意看到它。

編輯:我的解決方案改變了很多。再說一次,如果有更好的方法可以做到這一點;我非常願意接受有助於我提高整體知識/技能的建議。

相關內容