有什麼辦法可以縮短這個 Powershell 線嗎? (速記)

有什麼辦法可以縮短這個 Powershell 線嗎? (速記)

只是想看看是否有人知道縮短此 powershell 行的方法:

gci -Recurse -path $temp | ? {(".jpg",".png" -eq $_.extension -and $_ -match 'this' -and $_ -match "that")} | mi -Destination ($Images) -Force

該行在這段程式碼中:

$Temp = "F:\Temp-Images"
$Images = "F:\Images\"
gci -Recurse -path $Temp | ? {(".jpg",".png" -eq $_.extension -and $_ -match 'this' -and $_ -match "that")} | mi -Destination ($Images) -Force

該腳本旨在查找資料夾 ($Temp),查找擴展名為 .jpg 的任何文件或者.png,並且有兩個都檔案名稱中的單字「this」和「that」。如果找到匹配項,則會將檔案移至目標資料夾 ($Images)

因此,如果“F:\Temp-Images”中有一個名為“this-that.png”的文件,它將被移動到“F:\Images\”

我是 PowerShell 新手,所以我透過谷歌搜尋將上面的程式碼拼湊在一起。我不知道我的文法是否好,但它對我有用。我只是想知道是否有辦法縮短程式碼。例如,有沒有辦法縮短這個?

-and $_ -match 'this' -and $_ -match "that"

謝謝!

答案1

盡可能多做空會變得神秘;-)

$Temp = "F:\Temp-Images\"
$Images = "F:\Images\"
ls $Temp -R -I *.jpg,*.png|?{$_.Name -match 'this.*that|that.*this'}|mi -D ($Images) -Fo

為了測試我將附加-WhatIf-Confirm到 Move-Item

相關內容