一种顺序移动文件的方法吗?

时间:2014-02-20 18:53:05

标签: macos shell applescript

以下AppleScript非常适合将用户定义数量的文件移动到创建的文件夹中。 如果文件是用填充编号的; 001.txt 002.txt 003.txt,一切都很好。

但如果将它们命名为1.txt 2.txt 3.txt,则结果为1.txt 10.txt 11.txt位于同一文件夹中。

有没有办法通过shell移动文件来避免这种情况?

    tell application "Finder" to set thisDir to (target of Finder window 1) as string
    set rootDirectory to quoted form of POSIX path of thisDir
    set counTed to (do shell script "ls -1 " & rootDirectory & " | wc -l") as integer
    if counTed is 0 then
        display alert "There are no files in the root of this directory to move."
        return
    end if
    set filesPerFolder to text returned of (display dialog "There are " & counTed & " files in this folder. 
    How many files would you like to move per folder: " default answer "100")
    set fileCount to (do shell script "cd " & rootDirectory & " && i=0;for f in *;do d=$(printf %03d $((i/" & filesPerFolder & "+1)));let i++;mkdir -p $d;mv \"$f\" $d;done")
    set filesLeft to (do shell script "ls -2 " & rootDirectory & " | wc -l") as integer
    if filesLeft is 0 then
        display alert "Completed."
        return
    end if

1 个答案:

答案 0 :(得分:0)

为什么需要外壳? Finder有一个sort命令。因此,如果您想要根据需要正确排序文件列表,这样的工作就可以了。然后,您只需重复排序列表并移动任意数量的文件。

tell application "Finder"
    set thisDir to target of Finder window 1
    set theFiles to files of thisDir
    set sortedFiles to sort theFiles by name

    -- do something with sortedFiles
end tell