0

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

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 是否容易,我应该看什么

4

1 回答 1

3

这很容易,只是一开始并不明显。一些让你开始的事情。

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

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

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

加盐调味。

于 2010-10-06T18:53:14.127 回答