|
|
|||||||
Hey, I have a text file containing lots of “famous quotes”. I come to the idea, make a script that randomly picks up one of the quotes from the text file and print it out to the user, each time an event is preformed The text file: The reason a dog has so many friends is that he wags his tail instead of his tongue. --Unknown History is the version of past events that people have decided to agree upon. Napoleon Bonaparte Here's my strategy on the Cold War: We win, they lose." - Ronald Reagan Maturity is achieved when a person postpones immediate pleasures for long-term values. - Joshua L. Liebman quote And so on… I sow the RandomizeArray() udf, but don’t know how to adapt it in this case. Need some help on this! |
||||||||
|
|
|||||||
Don't randomize. That way you are going to get the same quotes repeated. Instead do one of the following:
|
||||||||
|
|
|||||||
Thanks Richard, I would be gratfull if you could provide som example, so I could better understand what you mean in both cases and how its done. Code:
|
||||||||
|
|
|||||||
Well, first you need to know the format of the file. Is it always exactly in the format of: Code: QUOTE i.e. there is always a single line of quote followed by a single line of attribution followed by a blank line? In quote files that I've seen there may be more than one file line in the quote. Also, what is the size of the file? Do you care if the file is reformatted to make it easier to read? Is this for your own use (in which you can write to it) or for general use from (say) a network share in which case you will probably want it to be read only. |
||||||||
|
|
|||||||
I can rearrange the quotes so each follows same standard. Conditions: - A quote could be more then one line, but always ends with a last line containing “-attribution”, followed by one empty line. The blank line will indicate the beginin or end of a quote. - The quote file could be formatted as needed to make it easy to read. - The quote file is for general use (several users) located at network share, BUT it is OK to have it open for write access if necessary. There is no need to have it read only. i.e. $File = “\\Myhome\Quotes\quotes.txt” Quotes format and order in “quotes.txt” The reason a dog has so many friends is that he wags his tail instead of his tongue. -Unknown History is the version of past events that people have decided to agree upon. -Napoleon Bonaparte Here's my strategy on the Cold War: We win, they lose." -Ronald Reagan Maturity is achieved when a person postpones immediate pleasures for long-term values. -Joshua L. Liebman |
||||||||
|
|
|||||||
...any sample tip how to begin? |
||||||||
|
|
|||||||
My recommendation since this is a per user thing would be to store a flag in the HKCU somewhere and read/write it as needed. Then store the quote file as a .ini in the NETLOGON and read it in that format sequentially. i.e. [QUOTES] 001=Wicked ways are those of a wicked mind 002=Lincoln was a penny pincher Then in the registry set 002 as the last one read and have your code increment up from there until you reach the end number then reset the value in the registry. |
||||||||
|
|
|||||||
Thanks Doc. I think I got your point using an ini file and read it with ReadProfileString, instead of regular txt file. But I’m not sure what you mean with the “flag in the HKCU… and increment up”, and how its done. Seems I need some simple codes to study and then understand… |
||||||||
|
|
|||||||
Well if no one else supplies anything I'll try to give you an example later on or tomorrow. |
||||||||
|
|
|||||||
Here you go: Code: Break ON This is how it works. Change $sQuoteTxt to point at your original file. Change $sQuoteIni to point to where you want the new, reformatted file to be written. When the script runs it checks to see if $sQuoteIni exists. If it doesn't, it will create it from the $sQuoteTxt file. The new file is in the INI style, and is lightly obfuscated so anyone looking at the file wont spoil the surprise of the quotes - you can remove the TXT file as once the INI file has been created the TXT file is no longer needed. The selection of the quote is based on the date, everyone will get the same quote on the same date which IMO is the best way to do it. Eagle-eyed date mathematicos will spot a fault in the formula, however it is sufficiently good for your needs. NB, once you have created the INI file there is no need to write to it again, so you can keep it on a read-only share. I've only tested on your short sample, so give it a go with your full quote file and let us know how you get on. |
||||||||
|
|
|||||||
What can I say! Beautiful artwork Richard! I’ve done quick test and it dose precisely what I had in mind. I just love the idea with encoding/decoding. I also tested @Time instead of @Year - (366*Left(@Time,2))+@YDAYNO) to get a quote per hour. The txt converting to ini works without any problem. I will now study the Convert function to understand how it works. 5 star Richard! |