AppleScript的 - 将文件移动到文件夹哪个名字开头的文件名的第7个字符 [英] Applescript - move files to folder which name begins with first 7 characters of filename

查看:685
本文介绍了AppleScript的 - 将文件移动到文件夹哪个名字开头的文件名的第7个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个文件夹

第一个文件夹包含所有这一切,我想移动到第二个文件夹的子文件夹中的文件。我想这基于两个文件和放大器的前7个字符;文件夹名。

所以,如果在文件夹2的子文件夹的前7个字符的文件的前7个字符匹配的文件夹1.该文件时的移动到文件夹2中的子文件夹

现在我有一个AppleScript它的工作原理,但它是非常缓慢的。

下面是脚本迄今:

 设置mgFileList为
设置mgDestFolder为设置mgFilesFolder到(选择带有提示的文件夹,文件存储在哪里,你想移动到目的地?)
设置mgDestFolder到(选择带有提示的文件夹哪里是目标文件夹?)
设置fold​​erList为mgDestFolder内容
告诉应用程序发现者来的fileList设置为mgFilesFolder的文件与å文件重复的fileList
设置preFIX获得preFIX(å文件名称)
告诉应用程序发现者     尝试
        设置destinationFolder为(mgDestFolder名称以preFIX的第一个文件夹中)
        移动å文件到destinationFolder
    年底试告诉结束
重复结束对GET preFIX(aName)
集preFIX文本1直通aName 7
返回preFIX
最终获取preFIX

我是最近才钻进Applescripting。这能中的AppleScript更有效地完成?我搜索周围它与一个shell脚本(我想可能会快很多),有效的解决方案,但一直没能得到其中的任何工作。

我工作的OSX山狮10.8.4

-
编辑

我看到,我已上载我已经设法到目前为止放在一起的脚本错误的版本。以上是工作的剧本,但它真的很慢。可能是因为它不使用任何shell脚本。

---------------更新-------------------

下面是工作pretty快,现在最终的脚本:

 设置mgFilesFolder选择文件夹提示,文件存储在哪里,你想移动到目的地?
设置mgDestFolder选择具有提示文件夹哪里是目标文件夹?告诉应用程序系统事件
设置为的fileList其mgFilesFolder名字不启动的文件。与å文件重复的fileList
    preFIX设置为我的get preFIX(å文件名称)
    尝试
        设置destinationFolder为(mgDestFolder名称以preFIX的第一个文件夹中)
        移动å文件到destinationFolder的POSIX路径
    年底试
重复结束
告诉结束
对GET preFIX(aName)
尝试
    集preFIX文本1直通aName 7
    返回preFIX
出错
    返回aName
年底试
最终获取preFIX


解决方案

搜索器通常是一个缓慢的程序,因为它做了很多事情在您的计算机上。因此,你应该尽量避免使用它。在你的情况下,这些任务都可以通过系统事件来完成。试试这个,也许会更快。请注意,您必须在code一对夫妇的错误,这是固定在这个code。

 设置mgFilesFolder选择文件夹提示,文件存储在哪里,你想移动到目的地?
设置mgDestFolder选择具有提示文件夹哪里是目标文件夹?告诉应用程序系统事件
    设置为的fileList其mgFilesFolder名字不启动的文件。    与å文件重复的fileList
        preFIX设置为我的get preFIX(å文件名称)
        尝试
            设置destinationFolder为(mgDestFolder名称以preFIX的第一个文件夹中)
            移动å文件到destinationFolder的POSIX路径
        年底试
    重复结束
告诉结束
对GET preFIX(aName)
    尝试
        集preFIX文本1直通aName 7
        返回preFIX
    出错
        返回aName
    年底试
最终获取preFIX

I've got 2 folders

the first folder contains all the files which I'd like to move to a subfolder of the second folder. I'd like to this based on the first 7 characters of both the file & folder names.

So if the first 7 characters of the subfolder in FOLDER 2 matches the first 7 characters of the file in FOLDER 1. The file get's moved into the subfolder of FOLDER 2

Right now I've got an Applescript which works, but it's terribly slow.

Here's the script so far:

set mgFileList to ""
set mgDestFolder to ""

set mgFilesFolder to (choose folder with prompt "Where are the files stored which you   would like to move to the destination?")
set mgDestFolder to (choose folder with prompt "Where is the destination folder?")


set folderList to contents of mgDestFolder


tell application "Finder" to set fileList to files of mgFilesFolder

repeat with aFile in fileList
set prefix to getPrefix(name of aFile)
tell application "Finder"

     try
        set destinationFolder to (1st folder of mgDestFolder whose name begins with prefix)
        move aFile to destinationFolder
    end try

end tell
end repeat



on getPrefix(aName)
set prefix to text 1 thru 7 of aName
return prefix
end getPrefix

I've only recently got into Applescripting. Can this be done more efficiently within Applescript? I've searched around for a solution which works with a shell script (which I think will probably be a lot faster), but haven't been able to get any of them working.

I'm working on OSX Mountain Lion 10.8.4

-- edit

I see that I've uploaded the wrong version of the script I've managed to put together so far. Above is the script that is working, but it's really slow. Probably because it doesn't use any shell script.

---------------update-------------------

Here is the final script that is working pretty fast right now:

set mgFilesFolder to choose folder with prompt "Where are the files stored which you would like to move to the destination?"
set mgDestFolder to choose folder with prompt "Where is the destination folder?"

tell application "System Events"
set fileList to files of mgFilesFolder whose name does not start with "."

repeat with aFile in fileList
    set prefix to my getPrefix(name of aFile)
    try
        set destinationFolder to (1st folder of mgDestFolder whose name  begins with prefix)
        move aFile to POSIX path of destinationFolder
    end try
end repeat
end tell


on getPrefix(aName)
try
    set prefix to text 1 thru 7 of aName
    return prefix
on error
    return aName
end try
end getPrefix

解决方案

The Finder is generally a slow program because it does a lot of things on your computer. Therefore you should avoid using it if possible. In your case these tasks can be done using System Events. Try this, maybe it will be faster. Note that you had a couple mistakes in your code which are fixed in this code.

set mgFilesFolder to choose folder with prompt "Where are the files stored which you would like to move to the destination?"
set mgDestFolder to choose folder with prompt "Where is the destination folder?"

tell application "System Events"
    set fileList to files of mgFilesFolder whose name does not start with "."

    repeat with aFile in fileList
        set prefix to my getPrefix(name of aFile)
        try
            set destinationFolder to (1st folder of mgDestFolder whose name begins with prefix)
            move aFile to POSIX path of destinationFolder
        end try
    end repeat
end tell


on getPrefix(aName)
    try
        set prefix to text 1 thru 7 of aName
        return prefix
    on error
        return aName
    end try
end getPrefix

这篇关于AppleScript的 - 将文件移动到文件夹哪个名字开头的文件名的第7个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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