Bir .NET yöntemi kullanabilirsiniz Görüntü boyutlarını elde etmek için O zaman herhangi bir üçüncü parti araçlara veya kütüphaneye güvenmeniz gerekmeyecek. İşte, bir System.Drawing.Image object oluşturmak ve boyutlarına erişmek için [System.Drawing.Image]::FromFile()
'u çağırmak üzere PowerShell karma kodunu kullanan bir örnek. Bir .bat uzantısı ile kaydedin ve ne düşündüğünüzü görün. Aslında
<# : Batch portion (PowerShell multi-line comment)
@echo off & setlocal
rem # move landscape pics to what directory?
2>NUL md landscape
for %%I in (*.jpg *.bmp *.gif *.png) do (
call :isLandscape "%%~fI" && (
rem # remove "echo" when satisfied that this works as desired
echo move "%%~fI" landscape\
) || (
echo %%~fI was portrait
)
)
goto :EOF
:isLandscape <imgfile>
rem # function sets errorlevel 0 if landscape, 1 if portrait or square
setlocal disabledelayedexpansion
set "img=%~1"
powershell -noprofile -noninteractive "iex (${%~f0} | out-string)"
endlocal & exit /b %errorlevel%
: end batch/begin PowerShell hybrid code #>
add-type -Assembly System.Windows.Forms
$size = [Drawing.Image]::FromFile((gi $env:img)).Size
exit ($size.Width -le $size.Height) # 0 = false, 1 = true
, toplu kapsamında dosyaların fazla işlem bağlı olduğu değilse, tek PowerShell oturumdaki dosyalar arasında çok daha hızlı döngü olacak. Bunu bir .bat veya bir .ps1 uzantısıyla kaydedin ve farkı göreceksiniz.
<# : batch portion
@echo off & setlocal
powershell -noprofile -noninteractive "iex (${%~f0} | out-string)"
goto :EOF
: end batch/begin PowerShell #>
add-type -Assembly System.Windows.Forms
$newdir = "landscape"
if (-not (test-path $newdir)) { md $newdir }
gci | ?{ $_.Name -match "\.(jpe?g|bmp|gif|png)$" } | %{
$size = [Drawing.Image]::FromFile($_).Size
if ($size.Width -gt $size.Height) {
# remove the -whatif flag to enable moves
mv $_.FullName $newdir -whatif
} else {
write-host ("{0} was portrait. Not moving." -f $_.FullName) -f cyan
}
}
Lütfen sorularınızı sadece (2) adetlere dahil etmek üzere – knocte