Applescript - 根据文件名的第一个单词创建文件夹 [英] Applescript - Creating folders based on the first word of the filename

查看:24
本文介绍了Applescript - 根据文件名的第一个单词创建文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本质上,我正在寻找一个 Applescript,它允许我通过创建仅包含文件第一个单词的文件夹来订购大量 50.000 个文件,而忽略第一个空格后的其余文件名.

Essentially i'm looking for an applescript that allow me to order the massive 50.000 files by creating folders that have just the first word of the files, ignoring the rest of the filename after the first space.

For eaxmple the 50.000 files are named like this:
 - amazingfrog -shootingbase.jpg 
 - frog 2sHDn1_9fFs12s.jpg
 - frog 29adjjdd39939.mov 
 - Horse IUS39aosdja.mov
 - horse 282131888.jpg 
 - HORSE.jpg
 And so on.....

- I would like to be like this:
    - amazingfrog
       -amazingfrog -shootingbase.jpg
    - frog    
       -frog 2sHDn1_9fFs12s.jpg 
       -frog 29adjjdd39939.mov
    - horse
       -horse IUS39aosdja.mov
       -horse 282131888.jpg 
       -horse.gif
And so on....

在互联网上我遇到了以下脚本:

On the internet i came across with the following script:

set chosenFolder to (choose folder)
tell application "Finder" to set fileList to files of chosenFolder

repeat with aFile in fileList
    set {name:Nm, name extension:Ex} to info for (aFile as alias)
    if Ex is missing value then set Ex to ""
    if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
    set dateFolder to text 1 thru 15 of Nm
    set sourceFile to quoted form of POSIX path of (aFile as text)
    set destinationFile to quoted form of (POSIX path of chosenFolder & dateFolder & "/" & name of aFile)
    do shell script "ditto " & sourceFile & space & destinationFile
    do shell script "rm " & sourceFile
end repeat

唯一的问题是我必须在text 1 thru"中选择我想保留的字母数字.不幸的是,文件名的第一个单词长度不同......

The only problem is that i have to choose in the "text 1 thru" the numbers of the letters i want to keep. And unfortunately the first word of the filenames have different length...

可以根据我的需要修改这个脚本吗?或者您有其他建议吗?

Could be possible to modify this script to my needed? or do you have any other suggestions?

提前感谢您的回复!!

推荐答案

我推荐使用文本项分隔符来提取文件名的第一部分

I recommend to use text item delimiters to extract the first part of the file name

set chosenFolder to (choose folder)
tell application "Finder" to set fileList to files of chosenFolder

set TID to text item delimiters
set text item delimiters to space
repeat with aFile in fileList
    set fileName to name of aFile
    set textItems to text items of fileName
    if (count textItems) = 1 then
        set fileExtension to name extension of aFile
        set folderName to text 1 thru ((get offset of "." & fileExtension in fileName) - 1) of fileName
    else 
        set folderName to first item of textItems
    end if
    set sourceFile to quoted form of POSIX path of (aFile as text)
    set destinationFile to quoted form of (POSIX path of chosenFolder & folderName & "/" & fileName)
    do shell script "ditto " & sourceFile & space & destinationFile
    do shell script "rm " & sourceFile
end repeat
set text item delimiters to TID

这篇关于Applescript - 根据文件名的第一个单词创建文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆