递归获取指定图片的大小与路径,排除含特定关键词的子文件夹
解决方案:批量筛选指定格式图片并排除特定目录
一、单条终端命令(推荐)
使用ExifTool可直接实现需求,命令如下:
exiftool -i HIDDEN -ext psb -ext psd -ext png -ext tif -ext jpg -filesize# -filename -if5 '$directory !~ /old|archive/i' -r [DIR]
参数说明:
-i HIDDEN:忽略隐藏文件与目录-ext [格式]:指定要筛选的图片格式(覆盖psb、psd、png、tif、jpg)-filesize#:输出纯数字格式的文件大小,便于后续批量处理-filename:输出文件的完整路径-if5 '$directory !~ /old|archive/i':过滤掉所在目录包含old或archive的文件,i表示不区分大小写-r [DIR]:递归遍历指定目录,将[DIR]替换为实际路径,例如/Users/xxx/Images
执行后终端会直接输出符合条件的图片路径与对应文件大小。
二、优化后的AppleScript方案
如果偏好使用AppleScript,以下脚本修复了原代码无法递归遍历、缺少排除规则的问题:
set targetFolder to choose folder with prompt "选择目标目录" set excludedKeywords to {"old", "archive"} set imageFormats to {"tif", "psd", "psb", "png", "jpg", "jpeg"} set resultList to {} tell application "Finder" -- 递归获取所有指定格式的文件 set allFiles to every file of entire contents of targetFolder whose name extension is in imageFormats repeat with aFile in allFiles set filePosixPath to POSIX path of aFile set folderPath to POSIX path of container of aFile set isExcluded to false -- 检查目录是否包含排除关键词(不区分大小写) repeat with keyword in excludedKeywords if (folderPath contains keyword) or (folderPath contains (keyword as uppercase)) or (folderPath contains (keyword as lowercase)) then set isExcluded to true exit repeat end if end repeat if not isExcluded then set fileSize to size of aFile set end of resultList to (filePosixPath & " | 大小:" & fileSize & " 字节") as string end if end repeat end tell -- 将结果保存到桌面文本文件 set outputFile to (path to desktop as string) & "图片信息汇总.txt" try open for access file outputFile with write permission write (join resultList with linefeed) to file outputFile starting at eof close access file outputFile display dialog "结果已保存至桌面的「图片信息汇总.txt」" on error try close access file outputFile end try end try
脚本改进点:
- 通过
entire contents实现递归遍历所有子目录 - 添加目录关键词排除逻辑,不区分大小写匹配
old/archive - 补充
jpeg格式以覆盖更多场景 - 将结果导出到桌面文本文件,方便查看和后续处理
内容的提问来源于stack exchange,提问作者Chris
相关产品推荐
相关产品推荐

