【问题标题】:Search folder for files with matching prefix and perform action in applescript在文件夹中搜索具有匹配前缀的文件并在 applescript 中执行操作
【发布时间】:2014-01-22 10:47:10
【问题描述】:

我有成千上万个 PDF 文件,我想找到符合某些特征并执行操作(合并为 PDF)的文件

例如,我有如下文件:

filegroup1_abc.pdf
filegroup2_xyz.pdf
filegroup3_qrs.pdf
filegroup3_lmn.pdf

我想找到每个具有相同前缀“filegroup1”的文件并将它们合并到一个 PDF 中,然后找到下一个匹配项 (filegroup2),然后是下一个,等等......

所以在我上面的例子中,最后两个将被合并为一个新的 PDF,因为它们具有“Filegroup3”前缀。

我找到了一个使用 applescript 合并 PDF 的脚本;所以我的主要问题是如何搜索文件夹,找到这些文件,然后对它们执行操作。但是“filetype1”的模式是未知的,因此脚本首先需要检查每个文件并比较文件名中前 X 个字符何时匹配。在这种情况下,前 22 个字符是相同的,表示文件之间的关系。

另外,我正在尝试在 applescript 中执行此操作,但使用其他方法可能更容易。

【问题讨论】:

  • 看来applescript支持正则表达式:stackoverflow.com/questions/997828/…
  • 谢谢@eebbesen,我需要它;但也许我的问题更基本,我如何找到前 X 个字符匹配的每个文件并对它们执行操作?

标签: applescript


【解决方案1】:
--choose working folder
set ff to quoted form of POSIX path of (choose folder)
try
    --returns files matching names as return-delimited text, filter to list of "paragraphs"
    -- using built-in AS object text awareness

[编辑]

    --OLD; incorrect:
    --set allABCs to every paragraph of (do shell script "cd " & ff & ";" & "ls filegroup*_qrs.pdf")

    -- new and improved:

    set allABCs to every paragraph of (do shell script "cd " & ff & ";" & "ls filegroup1_???.pdf")

    --that matches only 3-character strings. The following
    -- matches any number of characters between _ and .pdf:

    --set allABCs to every paragraph of (do shell script "cd " & ff & ";" & "ls filegroup1_*.pdf")

on error--it's in a try because if you mistakedly look for files that aren't there,
    --    it will return an error. As an aside, doing 'ls' and getting folders
    --    returns extra empty strings -- just a cautionary note that probably doesn't matter here
    return {}
end try

--还有一个您可能不需要的警告--如果这对您很重要,您可能需要对结果进行排序以正确排序文件

【讨论】:

  • 谢谢@CRGreen,这让我更接近了,但我需要知道通配符何时匹配,而不仅仅是生成一个列表。因此,在ls filegroup*_qrs.pdf 中,只要返回列表中的通配符相同,我就需要对那些匹配的文件执行操作。
  • 好的,@nocabt 我已经更新了代码。但是“当通配符匹配时”应该无关紧要。您应该能够为每个组生成要处理的每个文件列表。您可以根据自己的需要通过算法或一次 1 个来组装该组。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-01
  • 1970-01-01
相关资源
最近更新 更多