如何批量交换FolderX中点分隔命名PDF文件的文件名各位置字段
AppleScript 批量重命名点分隔字段文件名
可以通过AppleScript实现该需求,以下是开箱可用的脚本,支持自定义字段位置顺序:
脚本代码
-- 在这里自定义你需要的字段顺序,数值为原文件名从左到右的位置序号(从1开始计数) set customOrder to {4, 2, 5, 6, 3, 1} -- 选择目标文件夹 set targetFolder to choose folder with prompt "请选择FolderX目录" tell application "Finder" set allFiles to files of targetFolder repeat with singleFile in allFiles set originalName to name of singleFile set nameExt to name extension of singleFile -- 提取无后缀的文件名主体 if nameExt is not "" then set baseName to text 1 thru -((count of nameExt) + 2) of originalName else set baseName to originalName end if -- 按点拆分文件名主体为字段数组 set AppleScript's text item delimiters to "." set nameSegments to text items of baseName set AppleScript's text item delimiters to "" -- 校验字段数量是否满足自定义规则,不足则跳过当前文件 set maxPos to 0 repeat with pos in customOrder if pos > maxPos then set maxPos to pos end repeat if (count of nameSegments) < maxPos then continue repeat end if -- 按自定义顺序拼接新文件名主体 set newSegments to {} repeat with pos in customOrder set end of newSegments to item pos of nameSegments end repeat set AppleScript's text item delimiters to "." set newBaseName to newSegments as string set AppleScript's text item delimiters to "" -- 拼接后缀得到完整新文件名 if nameExt is not "" then set newName to newBaseName & "." & nameExt else set newName to newBaseName end if -- 执行重命名 set name of singleFile to newName end repeat end tell
自定义规则方法
- 修改脚本开头
customOrder数组的数值即可调整字段顺序,数值对应原文件名从左到右的位置序号(最左侧第一个字段为1) - 示例默认配置
{4, 2, 5, 6, 3, 1}对应需求pos4.pos2.pos5.pos6.pos3.pos1,示例文件GK.19.kkla.0715.nr.36053342.zg.119.pdf运行后将得到0715.19.nr.36053342.kkla.GK.pdf - 若仅需要交换指定位置:例如需要把pos3和pos6交换、pos1和pos4交换,其余位置保持原顺序,只需调整数组对应位置的数值即可
使用注意事项
- 运行前必须先备份所有目标文件,避免重命名错误导致文件丢失
- 建议先复制少量测试文件到单独文件夹运行脚本,确认重命名规则符合预期后再处理全量文件
- 脚本会自动跳过文件夹、以及字段数量不足你设置的规则的异常文件
- 如果需要保留原文件名中更多字段,只需在
customOrder数组中补充对应位置序号即可
内容的提问来源于stack exchange,提问作者salix
相关产品推荐
相关产品推荐

