Macos 一种通过shell顺序移动文件的方法?

Macos 一种通过shell顺序移动文件的方法?,macos,shell,applescript,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)

下面的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

你为什么需要贝壳?查找器有一个排序命令。因此,如果您想要获得一个文件列表,并按照您的要求进行适当排序,类似这样的方法是可行的。然后,您只需在已排序的列表上重复并移动任意数量的文件即可

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

如果有很多文件,Finder需要很长时间。使用shell要快得多我明白你的意思。您可以用系统事件替换查找器来处理大部分内容,这样会有所帮助。我认为不管怎样,您都必须执行某种类型的排序,如果这是真的,那么您派生的任何解决方案都会相对缓慢地对大量文件进行排序。祝你好运。。。我的回答只是一个想法。