Jump to content

Recommended Posts

Posted

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]

Posted

my intranet index page does a similar thing. sure you can modify it.

 

' 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("""" & QuoteArr(0) & """ - " & QuoteArr(1) & "")

 

think i got the basics for this somewhere on the net. don't recall precisely.

  • Thanks 1
Posted

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.

Posted

I was a bit bored tonight so I wrote you this little program.

 

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

  • Thanks 1
Posted
I was a bit bored tonight so I wrote you this little program.

 

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:

 

http://gyazo.com/179e05c145e062fb178ab35aa893bfe3.png

 

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]

Posted
my intranet index page does a similar thing. sure you can modify it.

 

' 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("""" & QuoteArr(0) & """ - " & QuoteArr(1) & "")

 

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:

 

http://gyazo.com/e2f6352d3b5ba2f25e3b73594b2bdd61.png

 

Any ideas?

Posted
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?

Posted

The script you need is

#!/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

script.pl infile >outfile

Posted
Thanks. However when I try to run this I get the following error:

 

http://gyazo.com/179e05c145e062fb178ab35aa893bfe3.png

 

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

  • Thanks 1
Posted (edited)

@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!
Edited by 6Foot2
Posted
@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 :)

Posted

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:

 

http://gyazo.com/750f2a85c8e176c61423be590289f2a3.png

 

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

 

Any ideas?

Posted (edited)

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

Edited by LosOjos
Posted
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?]
Posted
Strange that, not really sure what might have caused it. Anywho, don't know if you saw my edited post above, there's an updated version there that will dump any errors to the command line rather than them popping up, it won't disturb your students then :) plus if you get the error again, I'll have a better idea why if you tell me what the error says in the command line
Posted
Strange that, not really sure what might have caused it. Anywho, don't know if you saw my edited post above, there's an updated version there that will dump any errors to the command line rather than them popping up, it won't disturb your students then :) plus if you get the error again, I'll have a better idea why if you tell me what the error says in the command line

 

Excellent work! Thanks again.

Posted

@LosOjos [or anyone else who might be able to help with this]

 

Now that I can select and display randomly selected text with GetLine I had the thought that, perhaps, Getline can also help with another BGInfo problem I am having:

 

I get BGInfo to display some other information which helps me when assisting my users remotely. The station IP address is what I am interested in here. I have recently made changes as to how the IP address is accessed/displayed by BGInfo [because it would often display valid and non valid IPs on two lines: See below]

 

169.168.1.200

0.0.0.0

 

What it does now is write the valid IP address to a file and then query that. However for some reason the IP address is written to the file twice so that I get:

 

169.168.1.200,169.168.1.200

 

Is there any way that I can use GetLine to strip out only one copy of the valid IP or the left-most IP?

 

Many thanks.

Posted
Is there any way that I can use GetLine to strip out only one copy of the valid IP or the left-most IP?

 

Not in it's current state, but I'll gladly add in a command line option for you that allows you to return a certain column from a CSV (which is essentially what BGInfo seems to be returning).

 

Gimme half hour :)

  • Thanks 1
Posted (edited)
Not in it's current state, but I'll gladly add in a command line option for you that allows you to return a certain column from a CSV (which is essentially what BGInfo seems to be returning).

 

Gimme half hour :)

 

Absolutely stellar. I would add to your Rep but I have done that too recently for you so have a 'Thanks' instead.

 

Edit: BGInfo currently gets the IP address from a file called IP.TXT [if that matters?]

Edited by 6Foot2
Clarify post content.
Posted
BGInfo currently gets the IP address from a file called IP.TXT [if that matters?]

 

I imagine that IP.TXT is the file you want to pass to GetLine to get the IP address then.

 

Here is the new version with support for CSV/TSV files: GetLine.zip

 

Usage is as follows:

 

Usage:
    getline /IN:"Source.txt" /OUT:"Destination.txt"
             [/L:Line Number]
             [/COL:Column Number (CSV/TSV File)]

 

 

So for your needs, it's be something like:

 

getline /IN:"IP.TXT" /OUT:"REAL_IP.TXT" /L:1 /COL:1

 

 

EDIT: that's assuming of course that the IP address is on the first line and in the first 'column' (before the first comma).

 

Let me know if you have any trouble :)

  • Thanks 1
Posted (edited)

@LosOjos: Thanks again for your help.

 

I now have REALIP.TXT working. Now I just have to get BGInfo to show it which it is refusing to do at the moment.

 

Problem for another day. Time for a cooling pint...

 

Edit: Permissions for IP.TXT and RealIP.TXT differ. That could be the answer!

Edited by 6Foot2
Posted
Time for a cooling pint...

 

Sounds like a fantastic idea to me!

 

Always feel free to request more features, I'm an old skool geek who enjoys a little programming to break up my day :typing:

  • Thanks 1
  • 3 weeks later...
Posted

Hi LosOjos

 

I have just been trying your getline code out as I also had problems with reduandant IPs being displayed on some machines. Works great but a slight issue I have come across is on machines where there are not multiple IPs, the text in IP.txt will be something like 192.168.1.1. Then when you run getline on this it falls over and comes up with the message "This is not a comma/tab seperated file" and outputs nothing to RealIP.txt if this makes sense!

 

Would it be possible to add a check to the code where if only a single entry exists then it passes this onto RealIP.txt and if there are multiples it only passes the first entry?

 

Thanks for all your input!

 

Paul

  • 3 weeks later...
Posted

I have started to notice the same issue reported by ba9ag:

 

Where historically I could expect my IP.TXT file would contain something like:

 

192.168.1.201,192.168.1.201

 

some of my stations' IP.TXT files have only the first entry:

 

192.168.1.201

 

GetLine then gives the error:

 

This is not a comma/tab seperated file

 

How can I resolve this?

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...