使用AppleScript根据名称将文件夹中的文件排序到子文件夹
Sorting files in folder to subfolder based on name with AppleScript
我有几千个文件要分类到子文件夹中...
FILENAMES:(几个不同的扩展名)
- DK10xxx
- DK11xxx
- DK12xxx
使用 AppleScript:
repeat with i from 10 to 99
tell application "Finder"
set the_folder1 to folder "Sorting" of folder "Temp" of disk "HDD"
set the_folder2 to folder ("DK" & i) of folder "Sorting" of folder "Temp" of disk "HDD"
move (every item of the_folder1 whose name starts with ("DK" & i)) to the_folder2
end tell
end repeat
end run
结果:
- 错误"Finder got an error: AppleEvent timed out."数-1712
- 我必须重新启动 Finder
试试这个,它会在重复循环中检查源文件夹中的所有文件。
如果文件以 DK
开头,它会根据需要创建一个以文件名的前 4 个字符命名的文件夹,并将当前文件移动到子文件夹。
property sourceFolder : "HDD:Temp:Sorting"
tell application "Finder"
repeat with aFile in (get files of folder sourceFolder) as alias list
set fileName to name of aFile
if fileName starts with "DK" then
set prefix to text 1 thru 4 of fileName
if not (exists folder prefix of folder sourceFolder) then
make new folder at folder sourceFolder with properties {name:prefix}
end if
move aFile to folder prefix of folder sourceFolder
end if
end repeat
end tell
您收到的错误是超时错误。如果单个 Apple Event 花费的时间超过 2 分钟,就会发生这种情况。上面的代码试图通过使用更短的 Apple Events 来避免这个错误。
如果错误仍然存在(在重复行中)将重复循环包装到 with timeout
块中
with timeout of 1000000 seconds
repeat with aFile in (get files of folder sourceFolder) as alias list
...
end repeat
end timeout
我有几千个文件要分类到子文件夹中...
FILENAMES:(几个不同的扩展名)
- DK10xxx
- DK11xxx
- DK12xxx
使用 AppleScript:
repeat with i from 10 to 99
tell application "Finder"
set the_folder1 to folder "Sorting" of folder "Temp" of disk "HDD"
set the_folder2 to folder ("DK" & i) of folder "Sorting" of folder "Temp" of disk "HDD"
move (every item of the_folder1 whose name starts with ("DK" & i)) to the_folder2
end tell
end repeat
end run
结果:
- 错误"Finder got an error: AppleEvent timed out."数-1712
- 我必须重新启动 Finder
试试这个,它会在重复循环中检查源文件夹中的所有文件。
如果文件以 DK
开头,它会根据需要创建一个以文件名的前 4 个字符命名的文件夹,并将当前文件移动到子文件夹。
property sourceFolder : "HDD:Temp:Sorting"
tell application "Finder"
repeat with aFile in (get files of folder sourceFolder) as alias list
set fileName to name of aFile
if fileName starts with "DK" then
set prefix to text 1 thru 4 of fileName
if not (exists folder prefix of folder sourceFolder) then
make new folder at folder sourceFolder with properties {name:prefix}
end if
move aFile to folder prefix of folder sourceFolder
end if
end repeat
end tell
您收到的错误是超时错误。如果单个 Apple Event 花费的时间超过 2 分钟,就会发生这种情况。上面的代码试图通过使用更短的 Apple Events 来避免这个错误。
如果错误仍然存在(在重复行中)将重复循环包装到 with timeout
块中
with timeout of 1000000 seconds
repeat with aFile in (get files of folder sourceFolder) as alias list
...
end repeat
end timeout