PDA

View Full Version : Searching for Multiple File Types



TJ_Tigger
03-11-2004, 07:44 AM
I have been playing around with the code lately trying to find a good way to search for multiple file types and have it be dynamic. This is what I came up with and thought I would share. This will prompt the user for a directory and then search through that directory for the files contained in the table FileTypes. There is an option that requires a toggle button to define whether you will recurse the selected directory. The code is noted below. If you find files it will then add that information to a list box with the filename sans extension in the Item Text and the full path in the Item Data. What you do with it from there is yours to decide.

Comments are always welcome
Tigg



-- Display a folder browse dialog
sDirectory = Dialog.FolderBrowse("Please select a folder containing the audio files you wish to add to the ListBox.", "");

--[[ Get current state of Recurse option checkbox This option
requires a toggle button to be placed on your page to
identify if the user wants to recurse the folders If you do not
want to use this function, then replace the togState variable
in the File.Find actions below with either true or false]]--
togState = Button.GetState("recursetog");
--If the button is up then do not recurse
if togState == 0 then
togState = false
--If the button is Down then do recurse
else
togState = true
end
-- The togState variable will be use in the Find.File dialog below

-- Ensure the user did not press cancel
if sDirectory ~= 'CANCEL' then
--create the required tables
tFiles = {};
-- additional types can be added to this table to find more files
FileTypes = {"*.mp3","*.ogg","*.wav"};

--Enumerate the FileTypes table to find all the file types
for ftindex, ftvalue in FileTypes do
audioFiles = File.Find(sDirectory, ftvalue, togState, false, nil);

--if the audioFiles table contains something
if audioFiles then
for aIndex, aFilePath in audioFiles do
--Add the files from audioFiles to tFiles table
Table.Insert(tFiles, Table.Count(tFiles) + 1, aFilePath)
end
end
end

-- If tFiles contains something
if tFiles then
-- Traverse the table containing the selected file paths
for nIndex, sFilePath in tFiles do
-- Assign the desired text and data to variables
sData = sFilePath;
-- Set sText to the filename (without extension) using String.SplitPath
sText = String.SplitPath(sFilePath).Filename;
-- Add the item to the ListBox
ListBox.AddItem("ListBox1", sText, sData);
end
else
Dialog.Message("Error", "No files were found in the folder you specified", MB_OK, MB_ICONSTOP, MB_DEFBUTTON1);
end
end

Worm
03-11-2004, 08:00 AM
Nice!

Thanks, that'll be going into the scriplets section of my IDE fer sure!