PDA

View Full Version : help!searching files by filename with two keywords.



airpumpkin
04-06-2005, 09:34 PM
hi:
I want search files by filename with two keywords, how can I do it?
i.e:I want to search a file whose name contains 2005 and crach.
Please help me!

THX

Intrigued
04-06-2005, 11:25 PM
Here is a very simple example. I would recommend you bullet-proof the Input Object data checks (say, trim off the spaces from either side, etc. etc.) before using this is a critical project.

Here is the main code chunk:



-- Was the Enter key pressed? If so, continue on...
if e_Key == 13 then

strSearch1 = Input.GetText("Input1")
strSearch2 = Input.GetText("Input2")

-- Continue on if both Input Objects have data in them...
if strSearch1 ~= "" and strSearch2 ~= "" then

-- Get the file paths for all of the files in the AutoPlay/Docs (project) folder
tblFiles = File.Find("AutoPlay//Docs", "*.txt", false, true, nil, nil)

-- For each path in the table of paths generated do the following...
for index,value in tblFiles do

-- First, set a variable to hold one path out of the table (as we go through the table then we can work with just one file's path)
path = String.SplitPath(value)

-- Next, compare the data entered in the Input Objects to see if there is a match (case-insensitive) with any of the file names
-- The break;'s are in place so as to allow the script to continue on after a match (or lack of) is found
if String.CompareNoCase(strSearch1, path.Filename) == 0 or String.CompareNoCase(strSearch2, path.Filename) == 0 then
is_found = true
break;
else
is_found = false
break;
end

end
-- Give feedback to the end-user after we have checked for any file name matches against the two search terms
if (is_found) then
Dialog.Message("Results...", "There was a match with one of your search terms! ", MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1)
else
Dialog.Message("Results...", "No match found!", MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1)
end

end


-- Reset Input Objecst (to nothing)
Input.SetText("Input1", "")
Input.SetText("Input2", "")

-- Set focus to the first Input Object
Page.SetFocus("Input1")


end

airpumpkin
04-07-2005, 02:59 AM
THX a lot! :p

but if I have not only one file to show, how to deal with it?

TJ_Tigger
04-07-2005, 08:40 AM
THX a lot! :p

but if I have not only one file to show, how to deal with it?

Do you mean to say you want to search for more than one file type? In Intrigueds example it will find all the txt files in the specified directory and your search will take place in respect to all of those files. If you want more than one file type perform multiple File searches and then either run your search against all of those tables or combine all of your tables into one table and search that one table.

airpumpkin
04-07-2005, 08:53 AM
thx Tiger.

I meant that I have not only one file matches the two keywords. in Worm's reply, it shows that only one file is shown to the user.

TJ_Tigger
04-07-2005, 09:28 AM
thx Tiger.

I meant that I have not only one file matches the two keywords. in Worm's reply, it shows that only one file is shown to the user.

Thanks for elaborating. Try this



-- Was the Enter key pressed? If so, continue on...
if e_Key == 13 then

strSearch1 = Input.GetText("Input1")
strSearch2 = Input.GetText("Input2")

-- Continue on if both Input Objects have data in them...
if strSearch1 ~= "" and strSearch2 ~= "" then

-- Get the file paths for all of the files in the AutoPlay/Docs (project) folder
tblFiles = File.Find("AutoPlay//Docs", "*.txt", false, true, nil, nil)

-- For each path in the table of paths generated do the following...
--Build table to store found file in
tbFoundFile = {};
for index,value in tblFiles do

-- First, set a variable to hold one path out of the table (as we go through the table then we can work with just one file's path)
path = String.SplitPath(value)

-- Next, compare the data entered in the Input Objects to see if there is a match (case-insensitive) with any of the file names
-- The break;'s are in place so as to allow the script to continue on after a match (or lack of) is found
if String.CompareNoCase(strSearch1, path.Filename) == 0 or String.CompareNoCase(strSearch2, path.Filename) == 0 then
Table.Insert(tbFoundFiles, Table.Count(tbFoundFiles) +1, value);
end

end
-- Give feedback to the end-user after we have checked for any file name matches against the two search terms
--check to see if the table has items in it
if Table.Count(tbFoundFiles) > 0 then is_found = true end
if (is_found) then
Dialog.Message("Results...", "There was a match with one of your search terms! ", MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1)
else
Dialog.Message("Results...", "No match found!", MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1)
end

end


-- Reset Input Objecst (to nothing)
Input.SetText("Input1", "")
Input.SetText("Input2", "")

-- Set focus to the first Input Object
Page.SetFocus("Input1")


end