根据特定条件删除多个图像文件

根据特定条件删除多个图像文件

我正在寻找一种简单的方法来删除“壁纸”文件夹中尺寸小于 1080x1920 的图像。我认为可能存在某种脚本/命令可以做到这一点。我曾经在 Windows 本身中遇到过一个脚本功能,但我真的不太了解它。

任何帮助,将不胜感激!

答案1

实现此目的的最佳方法是使用外置工具下载它(请务必获取 Windows 可执行文件,而不是首先列出的 Linux 文件)。打开编辑器,粘贴以下代码并将其保存为“deletesmallwp.bat”(或相应地更改“filename”值)

@echo off
REM SETLOCAL EnableDelayedExpansion

REM Writes all img filenames smaller 1920/1080px for either a given parameter or a predefined folder into a txt file, then reads it and deletes all files written in there.
REM Without -r, not recursive, so without sub-folders.
REM "exifpath" needs to be changed to the path of the exiftool and "picpath" to the path of the pictures to be checked and deleted.
REM No need to run as admin, as long as you have the proper file permissions to delete (change)

ECHO Parameter: %1 

REM Paths can be either absolute or relative to the folder of this batch file. See f.e. exifpath 
REM In this ex. the batch is saved in "Pictures" and the pics to delete are in "Pictures\Wallpapers"
set exifpath=C:\Users\User\Pictures\exif\exiftool.exe
set exifpath=exif\exiftool.exe
set picpath=Wallpapers
set exportfile=smallwp.txt
set filename=deletesmallwp

REM Check from where file was run and set working directory if needed.
set batchpath=%0
set res=
IF %batchpath% == %filename% set res=1
IF "%batchpath%" == "%filename%.bat" set res=1
IF NOT DEFINED res (
    cd /D %~dp0
)

REM Check if file exists, else skip to the end
IF NOT EXIST %exifpath% GOTO NOPORT

REM Some info
IF [%1] EQU [] ( ECHO UNDEFINED)

ECHO.
IF NOT DEFINED res ECHO INFO: Changed working directory
ECHO.

REM Check if a parameter is given, then run exiftool to create a list with images
IF [%1] EQU [] (
    ECHO -Direct: '%picpath%'
    %exifpath% -if "$imagesize and ($imagewidth<1920 or $imageheight<1080)" -filename -T -L %picpath% >%exportfile%
) ELSE (
    ECHO -Parameter 'see above'
    %exifpath% -if "$imagesize and ($imagewidth<1920 or $imageheight<1080)" -filename -T -L %1 >%exportfile%
)
ECHO.
ECHO If you don't want certain files to be deleted, now might be a good time to look into %exportfile%.
ECHO.
pause
ECHO.
REM REM Delete every file found in the file and print the success
for /F "tokens=*" %%A in (%exportfile%) do del "%picpath%\%%A" 2>&1 >nul|findstr "^" >nul && echo FAILURE with %%A || echo DELETED
ECHO.
goto :ENDE

:NOPORT
echo "Portable ExifTool missing"
goto :ENDE


:ENDE
pause

根据第一行注释 (REM) 更改设置值。然后运行它(从资源管理器或命令提示符(使用图片文件夹作为参数))。希望这是您一直在寻找的/代码足够容易理解。如果您需要任何进一步的帮助,最好在您的问题中添加评论。

相关内容