File.Find

table File.Find ( 

string   Folder,

string   Filename,

boolean  Recurse = false,

boolean  IncludeFolders = false,

function CallbackFunction = nil,

function FileFoundCallbackFunction = nil )

Example 1

DLL_Files = File.Find(_SourceFolder, "*.dll", true, false, ShowSearchProgress, nil);

Searches for DLL files inside the folder that the update is running from, and stores their paths in a table called DLL_Files. Since Recurse is true, the action will search inside subfolders. The function named ShowSearchProgress will be called whenever progress is made in the search operation.  No callback function is used when a file is found.

Example 2

found = File.Find(Shell.GetFolder(SHF_MYDOCUMENTS), "*.pdf", true, false);
if (found) then
    Dialog.Message("Found one!", "The first PDF file found was:" .. found[1]);
end

Searches for PDF files anywhere inside the user's My Documents folder, and stores the paths to any PDF files that are found in a table named "found." Then, the example tests that table to see if there were any files stored in it (if there weren't, it would have been set to nil by the File.Find action), and if there are, puts up a message dialog to show the path to the first PDF file that was found.

Example 3

directory_structure = File.Find(Shell.GetFolder(SHF_MYDOCUMENTS), "*.*", true, true);

Searches for all files inside of the user's My Documents folder. Since Recurse is true and IncludeFolders is true, the action will search inside subfolders and will return all files that were found as well as all folders that were encountered during the search.

Example 4

-- Gets the path to the user's My Documents folder.
my_docs_path = Shell.GetFolder(SHF_MYDOCUMENTS);

-- Check to see if the My Documents folder detection was successful.
if (Application.GetLastError() == 0)then

    -- Search the user's My Documents folder for the file "Data.ini".
    search_results = File.Find(my_docs_path, "Data.ini", true, false, nil);

    --Check to see if an error occurred during the search. If it did, display the error message.
    error = Application.GetLastError();
    if error ~= 0 then
        Dialog.Message("Error",_tblErrorMessages[error]);
    else

        -- If no files were found, notify the user.
        if (search_results == nil) then
            Dialog.Message("Notice", "Data.ini was not found in your My Documents folder.");

        -- If files were found, display a dialog containing a list of their locations.
        -- Also ask for deletion confirmation.
        else
            message = "Data.ini was found in the following location(s). Click OK to delete the file(s):\r\n\r\n";
            for index, path in search_results do
                message = String.Concat(message, path.."\r\n");
            end
            proceed = Dialog.Message("File Search Results", message, MB_OKCANCEL, MB_ICONQUESTION, MB_DEFBUTTON1);

            -- If the user clicked OK, delete all of the files found.
            if proceed == IDOK then

                -- Delete each file found in the search.
                for index, path in search_results do
                    File.Delete(path, false, false, false, nil);

                    -- Check to see if any errors occurred during the deletion.
                    if (Application.GetLastError() ~= 0) then
                        Dialog.Message("Error", "The file located at: "..path.." could not be deleted.");
                    end
                end
            end
        end
    end
end

This example uses the File.Find action to search for all copies of a particular INI file in the user's My Documents folder. If the file is found in more than one location, the user is presented with the list and asked if they wish to delete them. If they agree, each copy is deleted using the File.Delete action.

Example 5

-- Callback function, used when a match is found
function found (filename)
    -- Display a dialog to the user
    Dialog.Message("FOUND!!", filename .. " was found!!\r\nNow terminating find . . .");
    -- return false (end the find action)
    return false;
end


-- Show the status dialog
StatusDlg.Show();

-- Find any filename containing 'win'.  Call function 'found' whenever a match is found
File.Find("c:\\", "*win*.*", true, false, nil, found);

-- Hide the status dialog
StatusDlg.Hide();

This example searches the user's entire hard-drive for filenames containing 'win'.  When a match is found, the function 'found' is called, and the search is terminated.  Using the technique above, a File.Find action can be terminated as soon as one match is found, regardless of how many matches exist in the search location.

Example 6

g_FolderSize = 0;

-- This function determines the size of a file and adds it to the total.
-- This function is used as the callback function for the File.Find action in this example.
function OnFileFound(Path)
    local nSize = File.GetSize(Path);
    g_FolderSize = g_FolderSize + nSize;
    return true;
end

-- Allow the user to browse for a folder whose size they want.
local strFolder = Dialog.FolderBrowse("Find a folder",Shell.GetFolder(SHF_MYDOCUMENTS));

StatusDlg.Show();
tblFiles = File.Find(strFolder,"*.*",true,false,nil,OnFileFound);
StatusDlg.Hide();
Dialog.Message("Size",String.GetFormattedSize(g_FolderSize));

This example calculates the total size of a selected folder's contents. To accomplish this, the File.Find action is called with the callback function called "OnFileFound".

See also:  Related Actions