【问题标题】:Use Automator and Applescript to move files to folders based on File Name使用 Automator 和 Applescript 根据文件名将文件移动到文件夹
【发布时间】:2010-10-06 18:21:05
【问题描述】:

我有一个文件夹,其中包含以下文件:

Elephant.19864.archive.other.pdf
Elephant.17334.other.something.pdf
Turnip.19864.something.knight.pdf
Camera.22378.nothing.elf.pdf

我希望将这些文件移动到以下结构中

Archive
    Elephant
        Elephant.19864.pdf
        Elephant.17334.pdf
    Turnip
        Turnip.19864.pdf
    Camera.HighRes
        Camera.HighRes.22378.pdf

生成的文件由一个或多个单词组成,后跟一个数字序列,然后是其他单词,然后是扩展名。我想将它们移动到一个名为数字之前的单词或单词的文件夹中,并删除数字和扩展名之间的所有单词(在本例中为 .pdf)。

如果文件夹不存在,那么我必须创建它。

我认为使用 Automator 或 AppleScript 会很简单,但我似乎无法理解它。

如果是这样,使用 Automator/AppleScript 是否容易,我应该看什么

【问题讨论】:

    标签: applescript automator


    【解决方案1】:

    这很容易,只是一开始并不明显。一些可以帮助您入门的事情。

    要解析文件名以获取您的文件夹名称,您需要将名称分隔成一个列表...

    set AppleScript's text item delimiters to {"."}
    set fileNameComponents to (every text item in fileName) as list
    set AppleScript's text item delimiters to oldDelims
    --> returns: {"Elephant", "19864", "archive", "other", "pdf"}
    

    列表有一个从 1 开始的索引,因此第 1 项是“Elephant”,第 5 项是“pdf”。要将文件名混合在一起,那么您所需要的就是这个

    set theFileName to (item 1 of fileNameComponents & item 2 of fileNameComponents & item 5 of fileNameComponents) as string
    

    要创建文件夹,只需使用以下...

    tell application "Finder"
        set theNewFolder to make new folder at (theTargetFolder as alias) with properties {name:newFolderName, owner privileges:read write, group privileges:read write, everyones privileges:read write}
    end tell
    

    要移动文件,您只需要这个...

    tell application "Finder"
        set fileMoved to move theTargetFile to theTargetFolder
    end tell
    

    要重命名文件,请使用以下内容...

    set theFileToRename to theTargetFilePath as alias -- alias is important here
    set name of theFileToRename to theFileName
    

    我建议首先创建所有目标文件的列表,然后为列表中的每个文件根据其名称创建文件夹,移动文件,最后在其最终位置重命名。

    加盐调味。

    【讨论】:

      猜你喜欢
      • 2020-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多