+ Post New Thread
Page 1 of 3 123 LastLast
Results 1 to 15 of 31
How do you do....it? Thread, Select Single Paragraph From Text File At Random in Technical; Our school has lots of little phrases like: Every day is a gift. The best way to predict your future ...
  1. #1

    DaveP's Avatar
    Join Date
    Oct 2006
    Location
    Can't talk now: The mother-ship is calling!
    Posts
    8,712
    Thank Post
    351
    Thanked 1,268 Times in 866 Posts
    Blog Entries
    4
    Rep Power
    1124

    Select Single Paragraph From Text File At Random

    Our school has lots of little phrases like:

    Every day is a gift.
    The best way to predict your future is to create it.
    You will never find time for anything. If you want time you must make it.

    Our staff like to motivate our students by having these on the walls around the school.

    I had the thought that if I could put lots of these into a text file I could [somehow] run a query on the file and randomly choose one of the paragraphs and have that display on the student desktops using BGInfo [random choice because if they are all the same then the students would not find it as interesting/noticeable if they were all the same or they were predictable]

    So my question is:

    How do I query the file:

    \\Server\Share\Quotes.TXT

    pull out a single line, write that to another file so that BGInfo can display it?

    Hope that is clear. Can it be done?

    Thanks.

    Note: There would be LOTS of phrases to choose from not just two [hopefully hundreds]

  2. #2
    browolf's Avatar
    Join Date
    Jun 2005
    Location
    Mars
    Posts
    1,524
    Thank Post
    106
    Thanked 88 Times in 74 Posts
    Blog Entries
    46
    Rep Power
    40
    my intranet index page does a similar thing. sure you can modify it.

    Code:
    ' Make a file called "Quotes.txt"
    ' Quotes.txt format:
    ' quote text1|author1
    ' quote text2|author2
    ' Example:
    ' Never miss a good chance to shut up.|Will Rogers
    
    ' The next line initializes all the variables
    Dim MyPath, FConn, File, TmpStr, CNT
    Dim QuoteArr, QuoteText, QuoteAuthor, LineArr
    ReDim LineArr(0)
    
    ' This line should be your path to the Quotes.txt file
    MyPath = "quotes.txt"
    
    ' These lines open the text file for reading
    Set FConn = Server.CreateObject("Scripting.FileSystemObject")
    Set File = FConn.OpenTextFile(Server.MapPath(MyPath),1)
    
    CNT = 0
    
    ' This reads in all the lines of the file
    Do While Not File.AtEndOfStream
    TmpStr = File.ReadLine
    ReDim Preserve LineArr(CNT)
    LineArr(CNT) = TmpStr
    CNT = CNT + 1
    Loop
    ' Close the file
    File.Close
    Set File = Nothing
    Set FConn = Nothing
    
    ' This finds a random quote from the array
    Randomize()
    QuoteArr = Split(LineArr(Int(RND() * Ubound(LineArr))),"|")
    
    QuoteText = QuoteArr(0)
    QuoteAuthor = QuoteArr(1)
    
    ' The output will look like "quote text here - author"
    Response.Write("<font face='verdana' size='-1' color='green'><i>""" & QuoteArr(0) & """</i> - " & QuoteArr(1) & "</font>")
    think i got the basics for this somewhere on the net. don't recall precisely.

  3. Thanks to browolf from:

    DaveP (7th July 2010)

  4. #3

    DaveP's Avatar
    Join Date
    Oct 2006
    Location
    Can't talk now: The mother-ship is calling!
    Posts
    8,712
    Thank Post
    351
    Thanked 1,268 Times in 866 Posts
    Blog Entries
    4
    Rep Power
    1124
    I will give that a go. However it does not appear to match what I need. Can anyone get any closer? [I am not good at coding/adapting code]

    Thanks.

  5. #4

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,432
    Thank Post
    1,432
    Thanked 1,160 Times in 794 Posts
    Rep Power
    705
    I was a bit bored tonight so I wrote you this little program.

    Code:
    Usage:
           getline /IN:Source.txt /OUT:Destination.txt [/L:Line Number]

    pretty self explanatory I think, drop the "/L" paramater all together to return a random line, let me know if you have any trouble (I've also attached the source for anybody to check before they run it if they see fit )


    getline.zip

  6. Thanks to LosOjos from:

    DaveP (8th July 2010)

  7. #5

    DaveP's Avatar
    Join Date
    Oct 2006
    Location
    Can't talk now: The mother-ship is calling!
    Posts
    8,712
    Thank Post
    351
    Thanked 1,268 Times in 866 Posts
    Blog Entries
    4
    Rep Power
    1124
    Quote Originally Posted by LosOjos View Post
    I was a bit bored tonight so I wrote you this little program.

    Code:
    Usage:
           getline /IN:Source.txt /OUT:Destination.txt [/L:Line Number]

    pretty self explanatory I think, drop the "/L" paramater all together to return a random line, let me know if you have any trouble (I've also attached the source for anybody to check before they run it if they see fit )
    Thanks. However when I try to run this I get the following error:



    I have come across this error before and when I try to install the version of .NET frame work it says [something like] Windows 7 needed [can't remember exactly]

  8. #6


    tom_newton's Avatar
    Join Date
    Sep 2006
    Location
    Leeds
    Posts
    4,461
    Thank Post
    866
    Thanked 845 Times in 667 Posts
    Rep Power
    195
    If you would be willing to install Perl, this is a piece of cake, and i'd happily scrounge up the appropriate scriptage...

  9. #7

    DaveP's Avatar
    Join Date
    Oct 2006
    Location
    Can't talk now: The mother-ship is calling!
    Posts
    8,712
    Thank Post
    351
    Thanked 1,268 Times in 866 Posts
    Blog Entries
    4
    Rep Power
    1124
    Quote Originally Posted by browolf View Post
    my intranet index page does a similar thing. sure you can modify it.

    Code:
    ' Make a file called "Quotes.txt"
    ' Quotes.txt format:
    ' quote text1|author1
    ' quote text2|author2
    ' Example:
    ' Never miss a good chance to shut up.|Will Rogers
    
    ' The next line initializes all the variables
    Dim MyPath, FConn, File, TmpStr, CNT
    Dim QuoteArr, QuoteText, QuoteAuthor, LineArr
    ReDim LineArr(0)
    
    ' This line should be your path to the Quotes.txt file
    MyPath = "quotes.txt"
    
    ' These lines open the text file for reading
    Set FConn = Server.CreateObject("Scripting.FileSystemObject")
    Set File = FConn.OpenTextFile(Server.MapPath(MyPath),1)
    
    CNT = 0
    
    ' This reads in all the lines of the file
    Do While Not File.AtEndOfStream
    TmpStr = File.ReadLine
    ReDim Preserve LineArr(CNT)
    LineArr(CNT) = TmpStr
    CNT = CNT + 1
    Loop
    ' Close the file
    File.Close
    Set File = Nothing
    Set FConn = Nothing
    
    ' This finds a random quote from the array
    Randomize()
    QuoteArr = Split(LineArr(Int(RND() * Ubound(LineArr))),"|")
    
    QuoteText = QuoteArr(0)
    QuoteAuthor = QuoteArr(1)
    
    ' The output will look like "quote text here - author"
    Response.Write("<font face='verdana' size='-1' color='green'><i>""" & QuoteArr(0) & """</i> - " & QuoteArr(1) & "</font>")
    think i got the basics for this somewhere on the net. don't recall precisely.
    Hi.

    I have just tried to adapt the script that you suggested. I have added the path to my "quotes.txt" file at line 14 [that being the only change I have made]

    It errors out at line 17 as follows:



    Any ideas?

  10. #8

    DaveP's Avatar
    Join Date
    Oct 2006
    Location
    Can't talk now: The mother-ship is calling!
    Posts
    8,712
    Thank Post
    351
    Thanked 1,268 Times in 866 Posts
    Blog Entries
    4
    Rep Power
    1124
    Quote Originally Posted by tom_newton View Post
    If you would be willing to install Perl, this is a piece of cake, and i'd happily scrounge up the appropriate scriptage...
    Ok. I have just downloaded strawberry-perl-5.12.0.1.msi and I am installing it at a single station accepting all of the defaults.

    What now?

  11. #9


    tom_newton's Avatar
    Join Date
    Sep 2006
    Location
    Leeds
    Posts
    4,461
    Thank Post
    866
    Thanked 845 Times in 667 Posts
    Rep Power
    195
    The script you need is
    Code:
    #!/usr/bin/perl -Tw
    
    use strict;
    
    srand();
    my $line = "";
    rand($.) < 1 && ($line = $_) while(<>);
    print $line;
    Put this in a file with extension ".pl"

    then run
    Code:
    script.pl infile >outfile

  12. #10

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,432
    Thank Post
    1,432
    Thanked 1,160 Times in 794 Posts
    Rep Power
    705
    Quote Originally Posted by DaveP View Post
    Thanks. However when I try to run this I get the following error:



    I have come across this error before and when I try to install the version of .NET frame work it says [something like] Windows 7 needed [can't remember exactly]
    Sorry, knocked it together at home, forgot to change it to framework 3.5

    This version should work for you: GetLine.zip

  13. Thanks to LosOjos from:

    DaveP (8th July 2010)

  14. #11

    DaveP's Avatar
    Join Date
    Oct 2006
    Location
    Can't talk now: The mother-ship is calling!
    Posts
    8,712
    Thank Post
    351
    Thanked 1,268 Times in 866 Posts
    Blog Entries
    4
    Rep Power
    1124
    @LosOjos Working perfectly! Thank you. Now all I have to do is deploy it and get a list of suitable quotes from Staff/around school.

    Thanks again.

    Edit: Here is one I can't use on the network:

    I used all my sick days so I called in dead!
    Last edited by DaveP; 8th July 2010 at 11:36 AM.

  15. #12

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,432
    Thank Post
    1,432
    Thanked 1,160 Times in 794 Posts
    Rep Power
    705
    Quote Originally Posted by DaveP View Post
    @LosOjos Working perfectly! Thank you. Now all I have to do is deploy it and get a list of suitable quotes from Staff/around school.

    Thanks again.
    Always happy to help

  16. #13

    DaveP's Avatar
    Join Date
    Oct 2006
    Location
    Can't talk now: The mother-ship is calling!
    Posts
    8,712
    Thank Post
    351
    Thanked 1,268 Times in 866 Posts
    Blog Entries
    4
    Rep Power
    1124
    As I said in my last post it is working and I am very happy. However I have noticed this [non fatal] error popping up when students log in:



    Students have permissions to run GetLine and they have full control on the output file.

    Any ideas?

  17. #14

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,432
    Thank Post
    1,432
    Thanked 1,160 Times in 794 Posts
    Rep Power
    705
    Is the program running when the student logs in then? What is the command line at startup?


    EDIT: I know this isn't an ideal way of handling an unknown error, but as it's working for you and it's not a system critical process, I've just encased the whole program in a general exception handler (i.e. rather than receiving the aforementioned error message, the error will be written to the command line, so the student will be non the wiser): GetLine.zip
    Last edited by LosOjos; 8th July 2010 at 02:22 PM.

  18. #15

    DaveP's Avatar
    Join Date
    Oct 2006
    Location
    Can't talk now: The mother-ship is calling!
    Posts
    8,712
    Thank Post
    351
    Thanked 1,268 Times in 866 Posts
    Blog Entries
    4
    Rep Power
    1124
    I think I may have sorted it. It would appear that the problem arose whenever a Student logged onto a machine that had not been restarted since the changes to BGInfo.BGI had been made and so the output file did not yet exist. I think it is sorted now [possibly?]

SHARE:
+ Post New Thread
Page 1 of 3 123 LastLast

Similar Threads

  1. RM Ascend 2050 random text
    By dhoward_westexetc in forum Hardware
    Replies: 5
    Last Post: 6th November 2009, 06:12 PM
  2. Batch File (select random file) help
    By fil_b in forum Windows Server 2000/2003
    Replies: 5
    Last Post: 5th November 2009, 04:13 AM
  3. password protect a single file
    By rocknrollstar in forum Windows
    Replies: 15
    Last Post: 30th July 2009, 09:21 AM
  4. Outlook 2003 random e-mails not showing text
    By phreak in forum Office Software
    Replies: 0
    Last Post: 23rd July 2009, 03:30 PM
  5. Multiple People Accessing single file
    By Nij.UK in forum Windows
    Replies: 6
    Last Post: 5th June 2006, 08:52 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •