views:

43

answers:

2

Hi,

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
A: 

Try changing your recursive call to

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

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

Lizzan
A: 

Here is the solution I got which is working...

--find . -name "*.rar" -type f -delete

set folderToProcess to (choose folder with prompt "Choose Folder::")

tell application "Finder"
    activate
    set fileExt to {".rar"}
    set theTopFolder to (folderToProcess as alias)
    repeat with EachFile in (get every file of folder (folderToProcess as alias))
        try
            copy name of EachFile as string to FileName
            repeat with ext in fileExt
                if FileName ends with ext then
                    set result to (open EachFile)

                    --delete Eachfile
                    msg(result)
                end if
            end repeat
        end try
    end repeat
    --display dialog (theTopFolder as text)
    repeat with EachSubDir in (get every folder of folder theTopFolder)
        try
            --display dialog (EachSubDir as text)
            repeat with EachFile in (get every file of folder (EachSubDir as alias))
                try
                    copy name of EachFile as string to FileName
                    --display dialog FileName
                    --move Eachfile to theTopFolder
                    repeat with ext in fileExt
                        if FileName ends with ext then
                            --display dialog FileName
                            set result to (open EachFile)
                            --delete Eachfile
                            msg(result)
                        end if
                    end repeat
                end try
            end repeat
            --delete folder (EachSubDir as alias)
        end try
    end repeat
end tell
Teja Kantamneni