批处理脚本可将所有横向照片移至其自己的子目录中 [英] Batch script to move all landscape oriented photos into their own sub directory

查看:190
本文介绍了批处理脚本可将所有横向照片移至其自己的子目录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个包含一堆图像文件的文件夹. JPG,PNG等.我想创建一个批处理脚本,该脚本将:

Suppose I have a folder that contains a bunch of image files. JPG, PNG, etc. I want to create a batch script that will:

1)遍历每个文件
2)对于每个文件,检查图像宽度是否大于图像高度
3)如果为true,请移至名为landscape的子目录.

1) loop through each file
2) for each file, check if the image width is greater than the image height
3) if true, move into a sub-directory named landscape.

我知道如何做#1和#3,但是迷路于#2.

I know how to do #1 and #3, but am lost on how to go about #2.

推荐答案

您可以使用.NET方法获取图像尺寸.然后,您无需依赖任何第三方工具或库.这是一个使用PowerShell混合代码来调用 [System.Drawing.Image]::FromFile() 来构建

You could use a .NET method to get the image dimensions. Then you wouldn't need to rely on any 3rd party tools or libraries. Here's an example that uses PowerShell hybrid code to invoke [System.Drawing.Image]::FromFile() to construct a System.Drawing.Image object and access its dimensions. Save it with a .bat extension and see what you think.

<# : 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


实际上,如果您不愿意进一步处理批处理上下文中的文件,则在单个PowerShell会话中循环浏览文件会更快.使用.bat或.ps1扩展名保存此文件,您将看到区别.


Actually, if you aren't that attached to further processing of the files within the batch context, it'll be much faster to loop through the files within a single PowerShell session. Save this with either a .bat or a .ps1 extension and you'll see the difference.

<# : 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
    }
}

这篇关于批处理脚本可将所有横向照片移至其自己的子目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆