For loop 如何使用批处理脚本计算文件夹中的每个文件

For loop 如何使用批处理脚本计算文件夹中的每个文件,for-loop,batch-file,imagemagick,nested-loops,For Loop,Batch File,Imagemagick,Nested Loops,我在批处理脚本中使用imagemagick来评估一个包含jpg和png文件的文件夹。我想检查每个图像的打印大小,如果它符合我指定的标准,则输出到文本文件。我的代码没有按我希望的方式工作。它会将所有数据输出到文本文件,但前提是文件夹中的每个图像都符合正确大小的标准。我想让它评估每个单独的图像。当前,如果有1个图像不在条件内,则不会向文本文件写入任何内容 @echo off REM %f - filename REM %[size] - original size of image REM %W

我在批处理脚本中使用imagemagick来评估一个包含jpg和png文件的文件夹。我想检查每个图像的打印大小,如果它符合我指定的标准,则输出到文本文件。我的代码没有按我希望的方式工作。它会将所有数据输出到文本文件,但前提是文件夹中的每个图像都符合正确大小的标准。我想让它评估每个单独的图像。当前,如果有1个图像不在条件内,则不会向文本文件写入任何内容

@echo off

REM %f - filename
REM %[size] - original size of image
REM %W - page width
REM %[resolution.x] - X density (resolution) without units
REM %H - page height
REM %[resolution.y] - Y density (resolution) without units
REM \n - newline
REM 1 in = 0.393701 cm - for PNG cm to in conversion

set output="C:\Documents and Settings\mfishm000\Desktop\Image_size.txt"

echo File_name:Size:Width:Height > %output%

REM set width as variable
FOR /F %%x IN ('identify -format "%%[fx:W/resolution.x]" *.jpg') DO SET width=%%x
FOR /F %%x IN ('identify -format "%%[fx:W/resolution.x*0.393701]" *.png') DO SET width=%%x

REM set height as variable
FOR /F %%y IN ('identify -format "%%[fx:H/resolution.y]" *.jpg') DO SET height=%%y 
FOR /F %%y IN ('identify -format "%%[fx:H/resolution.y*0.393701]" *.png') DO SET height=%%y

REM check if width is less than 8.67 and height is less than 11.22. If yes then output them to txt file
IF %width% LSS 8.67 (
    IF %height% LSS 11.22 (
        identify -format "%%f:%%[size]:%%[fx:W/resolution.x]:%%[fx:H/resolution.y]\n" *.jpg >> %output%
        identify -format "%%f:%%[size]:%%[fx:W/resolution.x*0.393701]:%%[fx:H/resolution.y*0.393701]\n" *.png >> %output%
    )
)

我认为问题在于您需要单独处理每个文件。看起来最后处理的文件只有一个宽度/高度值。还没有测试,但这应该可以工作

    @ECHO OFF

    REM %f - filename
    REM %[size] - original size of image
    REM %W - page width
    REM %[resolution.x] - X density (resolution) without units
    REM %H - page height
    REM %[resolution.y] - Y density (resolution) without units
    REM \n - newline
    REM 1 in = 0.393701 cm - for PNG cm to in conversion

    set output="C:\Documents and Settings\mfishm000\Desktop\Image_size.txt"

    echo File_name:Size:Width:Height > %output%


    for /f %%a in ('dir /b ^| find ".jpg"') do call :processA "%%~a"
    for /f %%a in ('dir /b ^| find ".png"') do call :processB "%%~a"

    exit /B

    :processA
        FOR /F %%x IN ('identify -format "%%[fx:W/resolution.x]" %1') DO SET width=%%x
        FOR /F %%y IN ('identify -format "%%[fx:H/resolution.y]" %1') DO SET height=%%y
        IF %width% LSS 8.67 (
            IF %height% LSS 11.22 (
                identify -format "%%f:%%[size]:%%[fx:W/resolution.x]:%%[fx:H/resolution.y]\n" %1 >> %output%
            )
        )
    goto:eof

    :processB
        FOR /F %%x IN ('identify -format "%%[fx:W/resolution.x*0.393701]" %1') DO SET width=%%x
        FOR /F %%y IN ('identify -format "%%[fx:H/resolution.y*0.393701]" %1') DO SET height=%%y
        IF %width% LSS 8.67 (
            IF %height% LSS 11.22 (
                identify -format "%%f:%%[size]:%%[fx:W/resolution.x*0.393701]:%%[fx:H/resolution.y*0.393701]\n" %1 >> %output%
            )
        )
    goto:eof