0

我有2个文件夹

第一个文件夹包含我想移动到第二个文件夹的子文件夹的所有文件。我想根据文件和文件夹名称的前 7 个字符来做这个。

因此,如果 FOLDER 2 中子文件夹的前 7 个字符与 FOLDER 1 中文件的前 7 个字符匹配。文件 get 被移动到 FOLDER 2 的子文件夹中

现在我有一个可以工作的Applescript,但它非常慢。

这是到目前为止的脚本:

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

我最近才接触到 Applescripting。这可以在 Applescript 中更有效地完成吗?我一直在寻找一种适用于 shell 脚本的解决方案(我认为它可能会快很多),但无法让它们中的任何一个工作。

我正在开发 OSX Mountain Lion 10.8.4

- 编辑

我看到我上传了错误版本的脚本,到目前为止我已经设法将它们放在一起。上面是正在运行的脚本,但它真的很慢。可能是因为它不使用任何 shell 脚本。

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

这是目前运行速度非常快的最终脚本:

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
4

3 回答 3

1

Finder 通常是一个缓慢的程序,因为它在您的计算机上执行了很多操作。因此,您应该尽可能避免使用它。在您的情况下,这些任务可以使用系统事件来完成。试试这个,也许它会更快。请注意,您的代码中有几个错误已在此代码中修复。

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
于 2013-08-23T14:30:12.220 回答
0

使用 shell 脚本可能更容易:

cd FOLDER1; for f in *; do d=../FOLDER2/"${f:0:7}"*; [[ -d $d ]] && mv "$f" "$d"; done
于 2013-08-23T08:49:24.087 回答
0

不确定您正在处理多少个文件,但我用 10 个文件夹和约 100 个文件对此进行了测试,运行时间不到 4 秒。使用字符串比使用文件要快得多,因此脚本将文件/文件夹作为字符串获取并在需要时构建别名。

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?")

--Always use System Events whenever possible. It's WAY faster than Finder.
tell application "System Events"
    set folderList to name of folders of mgDestFolder
    set fileList to name of files of mgFilesFolder
end tell


repeat with i from 1 to (count folderList)
    set folderName to item i of folderList
    set filesToMove to {}
    repeat with j from 1 to (count fileList)
        set filename to item j of fileList
        if filename begins with folderName then
            set end of filesToMove to alias ((mgFilesFolder as string) & filename)
        end if
    end repeat

    --Can't use system events for moving files. You have to use Finder.
    tell application "Finder"
        move filesToMove to alias ((mgDestFolder as string) & folderName & ":")
    end tell
end repeat
于 2013-08-23T14:31:42.797 回答