2010-10-06 150 views
0

我有一个包含以下文件的文件夹:使用的Automator和AppleScript基于文件名的文件移动到文件夹

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若有的话,我应该看着

回答

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项是“大象”第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 

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

加盐调味。

相关问题