이 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을 처음 접했기 때문에 Google 검색을 통해 위 코드를 하나로 묶었습니다. 내 구문이 좋은지 아닌지는 모르겠지만 나에게는 효과가 있습니다. 코드를 단축할 수 있는 방법이 있는지 궁금합니다. 예를 들어, 이것을 단축하는 방법이 있습니까?:

-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

테스트하려면 Move-Item에 추가 -WhatIf하거나 추가하겠습니다.-Confirm

관련 정보