从子文件夹中删除所有内容

时间:2015-08-13 04:52:02

标签: applescript

下面是我的脚本(不要问我是怎么来的)。 我知道这很愚蠢。 我只需要最简单的脚本转到子文件夹并删除:

  1. 变体。一切都来自它。
  2. 变体。 .mp4文件
  3. 不删除子文件夹。

    感谢您的帮助。

    property source_folder : alias "Macintosh HD:Users:User:Pictures:4K Stogram:"
    property save_folder : path to trash folder as string
    
    # set source_folder to choose folder with prompt "Select the folder of folders containing images."
    # set save_folder to choose folder with prompt "Select the folder to save the images in."
    
    process_folder(source_folder)
    
    on process_folder(this_folder)
        set these_items to list folder this_folder without invisibles
        set container_name to name of (info for this_folder)
        repeat with i from 1 to the count of these_items
            set this_item to alias ((this_folder as Unicode text) & (item i of these_items))
            if folder of (info for this_item) is true then
                process_folder(this_item)
            else
                process_item(this_item, container_name, i)
            end if
        end repeat
    end process_folder
    
    -- this sub-routine processes files 
    on process_item(this_item, c, i)
        tell application "System Events"
            move this_item to save_folder
        end tell
    end process_item
    

2 个答案:

答案 0 :(得分:0)

使用“整个内容”功能获取所有文件,包括递归子文件夹内容。下面的6行脚本将取代您的完整脚本。

set SourceFolder to choose folder with prompt "Source folder"
set SaveFolder to choose folder with prompt "Save  folder"
tell application "Finder"
   set myFiles to every file of entire contents of SourceFolder
   move myFiles to SaveFolder
end tell

您还可以通过在第4行添加过滤器,通过扩展名“mp4”过滤所选文件。您可以设置更多扩展名列表!

set myFiles to every file of entire contents of SourceFolder whose name extension is "mp4"

答案 1 :(得分:0)

要在给定文件夹的所有子文件夹中递归删除每个文件,但保留文件夹结构,shell命令find非常合适,因为它比Finder快得多。

  • 如果属性deleteOnlyMP4设置为true,则只有扩展名为mp4的文件才会移至垃圾箱文件夹。
  • 如果属性deleteOnlyMP4设置为false,则所有文件都将移至垃圾箱文件夹。

    property deleteOnlyMP4 : true
    
    set source_folder to POSIX path of (path to pictures folder) & "4K Stogram"
    
    if deleteOnlyMP4 then
        do shell script "/usr/bin/find " & quoted form of source_folder & " -iname '*.mp4' -exec /bin/mv {} ~/.Trash \\;"
    else
        do shell script "/usr/bin/find " & quoted form of source_folder & " -type f -exec /bin/mv {} ~/.Trash \\;"
    end if