PDA

View Full Version : Reading random text files from folder



Jonas DK
07-10-2006, 04:34 PM
Hi

I'm working on a new project That I cant get started on.

It's a basic slideshow that has a static graphics and dynamic text.

The only thing that changes(on page timer) is the text.

I want it to take a random textfile from a given folder and display the file name as the heading and the file content as the text body.

I'm thinking that I need to index the content of the folder so that the AMS app. knows the number of textfiles, and then pick a random file to display??
.. am I wrong in thinking this...

But howdo I get the app to take a random file from the given folder and display the filename (without extension) as a label and the content of the file in a paragraph? Is it at all posible? I know it must be.

The power of AMS has suprised me several times in the past when it comes to what is posible and what is not.

Does anyone have an Idea on this one, or have any one done something simular and want to share what they did?

cheers,
Jonas

TJ_Tigger
07-10-2006, 05:26 PM
I'm thinking that I need to index the content of the folder so that the AMS app. knows the number of textfiles, and then pick a random file to display??
.. am I wrong in thinking this...

I would agree. Use File.Find to find the .txt file in the directory. This will create a table with all the files found.

But howdo I get the app to take a random file from the given folder and display the filename (without extension) as a label and the content of the file in a paragraph? Is it at all posible? I know it must be.

Since all the files are now contained in a table you can perform a Math.Random(1, Table.Count(mytable)) to generate your random number. Once you have that number then you can use TextFile.ReadToString(mytable[rnd]) to read the contents of the file to a string and String.SplitPath(mytable[rnd]) on the entry from the table that contains the list of files to extract the file name.

Hopefully that will get you started.

Tigg

Jonas DK
07-11-2006, 03:31 PM
maybe it's because its late here or I am just not strong on tables.

But I got got it as far as indexing the folder and doing the math.random.
This returns a string called rnd that contains a random number.

Lets say that the table indexed is MyTable

Then when calling the random file from the folder you use MyTable[rnd]

but rnd is a string and not an assosiative array so wont it look in the table for an entry called rnd indstead of reading the number from the string?

I have created a little app to try and find the correct combination.

cheers,
Jonas

TJ_Tigger
07-11-2006, 03:58 PM
I believe the issue is the following



labeltext = label[Filename];
Label.SetText("Label1", ""..labeltext);


Change the above code in the OnShow event to the following



Label.SetText("Label1", label.Filename);


or



labeltext = label["Filename"];
Label.SetText("Label1", labeltext);


By not quoting the word Filename AMS thinks you are trying to pass a variable as the name of the entry within the table. With it quoted it says that this is the associative entry I want you to use. Or you can use the dot notation for associative arrays.

For not being strong on table is was a good attempt at coding it. It took me a while to wrap my head around the concept of tables. Somedays they still confiuse me.

You may also want to put a Math.RandomSeed() in just before you generate the random number to make the random function a little bit more random.

Tigg

Jonas DK
07-12-2006, 02:40 PM
as always you save the day.

After reading your reply I dusted off my Training cd's that Corey made and had a look at them again. Havent used AMS sicne 6.0 was released.

Things seem to disapear from memory when you dont use it regularly.

But it's still the best damm software for the job out there.
I have never come across something as powerfull and still super user friendly and easy to use out of the box, then AMS.

Cheers for the help tigg.
I'll post the done app with graphics when I get it finnished hopefully in the next couple of weeks, so that every one in the future can see what we did.

wasim21k
05-30-2007, 03:06 PM
havent you finised it yet?;)

Jonas DK
06-05-2007, 02:52 PM
havent you finised it yet?;)

Not really, I ended up scrapping the app and doing some other work instead..

I do thing I have actually finished the sample with the code tigger sergiested and made it work . o O (Think I still have the ams file somewhere...)