PDA

View Full Version : KB: Making a Document Browser


Desmond
09-26-2003, 03:28 PM
<HTML> <HEAD> <TITLE>AutoPlay Media Studio 5.0 Knowledge Base</TITLE> </HEAD> <BODY> <h3>Making a Document Browser</h3> <b>Document ID: IR10050</b> <hr> The information in this article applies to: <ul> <li>AutoPlay Media Studio 5.0 Professional Edition</li> </ul> <hr> <h3>SUMMARY</h3> <p>This article describes how to make a document browser</p> <h3>DISCUSSION</h3> <p>As an example, we will create an application that has the user select a folder on his drive, and then populates a listbox object with all of the *.doc files within that directory. The user clicks on a file in the listbox object, and clicks the "Open" button to open the document.</p> <ol> <li>Create a project with one listbox object, and two button objects.<br> <br> </li> <li>Label Button1 "Load" and Button2 "Open".<br> <br> </li> <li>Insert the following code into the On Click event for Button1:<pre><code>--Disable listbox Updating ListBox.SetUpdate("ListBox1", false);
<br />
<br />--Get the desired folder to browse
<br />folder = Dialog.FolderBrowse("Open Folder", "C:\\");
<br />
<br />--populate a table with all the .doc files
<br />file = File.Find(folder, "*.doc", false, false, nil);
<br />
<br />--do the following for each file:
<br />for j,file_path in file do
<br /> --add the item to the listbox, with the name visible and path as data
<br /> ListBox.AddItem("ListBox1", String.SplitPath(file_path).Filename, file_path);
<br />end
<br />
<br />--Allow the listbox to display the updated content ListBox.SetUpdate("ListBox1", true);</code></pre> </li> <li>Insert the following code into the On Click event for Button2:<pre><code>selected = ListBox.GetSelected("ListBox1");
<br />for j,k in selected do
<br /> File.Open(ListBox.GetItemData("ListBox1", k),"", SW_SHOWNORMAL);
<br />end</code></pre> </li> </ol> <h3>MORE INFORMATION</h3> <p>KEYWORDS: AutoPlay Media Studio 5.0, Document, Browser </p> <hr> <FONT SIZE=1> Last reviewed: September 26, 2003<br> Copyright © 2003 <A HREF="http://www.indigorose.com" target="blank">Indigo Rose Corporation</a>. All rights reserved.<br> </FONT> </BODY> </HTML>

azmanar
10-26-2004, 05:48 PM
Hi,

I tested this example.

The FILE BROWSER OPENS. Good.

However, when I attempted to LOAD a FOLDER that contains NONE of the .doc files, this error message appears: ON CLICK, Line 11: attempt to call nil value.

How do I get rid of this error message? Better still, a message will appear saying that no such files, and tell us to try another folder.

Thanks

Azman

TJ_Tigger
10-26-2004, 06:48 PM
In the section where you enumerate the files.

for j,file_path in file do
--add the item to the listbox, with the name visible and path as data
ListBox.AddItem("ListBox1", String.SplitPath(file_path).Filename, file_path);
end


encapsulate that bit of code in an if statement that checks to make sure the table file contains data.


if file then
for j,file_path in file do
--add the item to the listbox, with the name visible and path as data
ListBox.AddItem("ListBox1", String.SplitPath(file_path).Filename, file_path);
end
end


That way you wont run the code unless you actually find files.

Tigg

azmanar
10-27-2004, 05:11 PM
Hi Tigg,

And this is what Adam suggested and works as well.



if file ~= nil then

--do the following for each file:
for j,file_path in file do
--add the item to the listbox, with the name visible and path as data
ListBox.AddItem("ListBox1", String.SplitPath(file_path).Filename, file_path);
end

--Allow the listbox to display the updated content
ListBox.SetUpdate("ListBox1", true);

else
Dialog.Message("Notice","No Documents were found in that directory");
end



Thanks Tigg.

Azman

azmanar
10-27-2004, 05:19 PM
Hi,

How would look for more than 1 file type. Say, I would like to search for .doc and .ppt at the same time from the same folder?

So, if this is the case, how could I actually display even the file extensions to differentiate between the 2 types.

Any ideas?

I'm not a programmer , so I need some practical ideas as a kick-start.

Thanks

Azman

TJ_Tigger
10-28-2004, 06:38 AM
Here is an example of what I have done in the past


--Get the desired folder to browse
folder = Dialog.FolderBrowse("Open Folder", "C:\\");
tbSearchFiles = {"*.doc", "*.ppt"};

for index,type in tbSearchFiles do
file = File.Find(folder, type, false, false, nil);

if file ~= nil then

--do the following for each file:
for j,file_path in file do
--add the item to the listbox, with the name visible and path as data
ListBox.AddItem("ListBox1", String.SplitPath(file_path).Filename, file_path);
end

else
Dialog.Message("Notice","No Documents were found in that directory");
end
end

--Allow the listbox to display the updated content
ListBox.SetUpdate("ListBox1", true);




Just add the file for which you want to search to the tbSearchFiles table and let 'er rip, so to speak.

Hope this helps.

If I can find it again, this is also on the forums somewhere out there.

Tigg

TJ_Tigger
10-28-2004, 06:40 AM
Here is another version of the above code that I posted on the forums before

http://www.indigorose.com/forums/showthread.php?t=6864

azmanar
10-29-2004, 06:47 AM
Hi Tigg,

Both scripts worked very well.

One item is missing, i.e. The File Extension.

When we list in the ListBox only the filenames can be seen. What do we need to do to show the File Extensions?

Thanks

Azman

TJ_Tigger
10-29-2004, 12:02 PM
The splitpath command was used when added to a listbox and it did not include the extension. Try this.


--Get the desired folder to browse
folder = Dialog.FolderBrowse("Open Folder", "C:\\");
tbSearchFiles = {"*.doc", "*.ppt"};

for index,type in tbSearchFiles do
file = File.Find(folder, type, false, false, nil);

if file ~= nil then

--do the following for each file:
for j,file_path in file do
--add the item to the listbox, with the name visible and path as data
tbFilePath = String.SplitPath(file_path);
ListBox.AddItem("ListBox1", tbFilePath.Filename..tbFilePath.Extension, file_path);
end

else
Dialog.Message("Notice","No Documents were found in that directory");
end
end

--Allow the listbox to display the updated content
ListBox.SetUpdate("ListBox1", true);

upena
01-14-2005, 07:02 AM
Aloha

Working with listbox in a DEMO (before I shell out my money). I understand the script - but - how do I make it look at multiple drives for a specific folder. Not knowing which drive the CD will be in on different machines - but knowing the folder name, how do I do this?

I tried
result = Dialog.FolderBrowse("Please select a folder:", "AutoPlay\\MyFolderName");
and modified this line to look for .ZIP files instead of .doc files
file = File.Find(folder, "*.zip", false, false, nil);

When I click "Load" - it opens a pop-up that lists my "C:/My Documents" instead of going to the Folder that I specified. Any ideas? Or is this just because it is in Preview mode and only looking at what I created and saved on my "C" drive.

Also, is it possible to open a .zip and then open a .pdf or .doc or .whatever after - all in one smooth move?

Please bear with me - I am still learning.

Thanks in advance

Lar