AppleScript 递归处理文件夹中的文件 [英] AppleScript Processing Files in Folders recursively

查看:34
本文介绍了AppleScript 递归处理文件夹中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个根文件夹,里面有子文件夹.它通常只有一层,但可以更深.这些文件夹将包含不同的文件,包括一些 .rar 文件.我想创建一个遍历文件夹的递归函数,检查文件是否为 rar 文件并打开/提取它.代码工作到第一级,没有任何问题.但是递归调用不起作用并且苹果脚本的错误处理很糟糕.这是我到目前为止所做的代码.

I have a root folder and there are sub folders in it. It is generally one level only but it can be deeper. These folders will have different files including some .rar files. I want to create a recursive function which traverses the folders, check if the file is a rar file and open/extract it. The code is working to first level with out any problem. But the recursive call is not working and apple script's error handling is horrible. Here is the code which I have done so far.

set folderName to "Macintosh HD:Users:Teja:Desktop:Madhu Babu:"

process_folder("", folderName)

on process_folder(root, folderNameToProcess)
    set fileExt to {".rar"}
    tell application "Finder"
        set theItems to every file of folder (root & folderNameToProcess)
        repeat with theFile in theItems
            copy name of theFile as string to FileName
            repeat with ext in fileExt
                if FileName ends with ext then
                    open theFile
                    delete theFile
                end if
            end repeat
        end repeat
        set theFolders to name of folders of folder (root & folderNameToProcess)
        repeat with theFolder in theFolders
            copy theFolder as string to TheFolderName
            display dialog (folderNameToProcess & TheFolderName & ":")
            try
                process_folder(folderNameToProcess, TheFolderName & ":")
            on error errStr number errorNumber
                display dialog errStr
            end try
        end repeat
    end tell
end process_folder

推荐答案

尝试将递归调用更改为

Try changing your recursive call to

try
    my process_folder(folderNameToProcess, TheFolderName & ":")
on error errStr number errorNumber
    display dialog errStr
end try

注意process_folder 调用之前的my.这使它对我有用.

Note the my before the process_folder call. This made it work for me.

这篇关于AppleScript 递归处理文件夹中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆