6Foot2 Posted July 7, 2010 Posted July 7, 2010 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]
browolf Posted July 7, 2010 Posted July 7, 2010 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. 1
6Foot2 Posted July 7, 2010 Author Posted July 7, 2010 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.
LosOjos Posted July 7, 2010 Posted July 7, 2010 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 1
6Foot2 Posted July 8, 2010 Author Posted July 8, 2010 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]
tom_newton Posted July 8, 2010 Posted July 8, 2010 If you would be willing to install Perl, this is a piece of cake, and i'd happily scrounge up the appropriate scriptage...
6Foot2 Posted July 8, 2010 Author Posted July 8, 2010 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?
6Foot2 Posted July 8, 2010 Author Posted July 8, 2010 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?
tom_newton Posted July 8, 2010 Posted July 8, 2010 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
LosOjos Posted July 8, 2010 Posted July 8, 2010 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 1
6Foot2 Posted July 8, 2010 Author Posted July 8, 2010 (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 July 8, 2010 by 6Foot2
LosOjos Posted July 8, 2010 Posted July 8, 2010 @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
6Foot2 Posted July 8, 2010 Author Posted July 8, 2010 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?
LosOjos Posted July 8, 2010 Posted July 8, 2010 (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 July 8, 2010 by LosOjos
6Foot2 Posted July 8, 2010 Author Posted July 8, 2010 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?]
LosOjos Posted July 8, 2010 Posted July 8, 2010 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
6Foot2 Posted July 8, 2010 Author Posted July 8, 2010 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.
6Foot2 Posted July 12, 2010 Author Posted July 12, 2010 @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.
LosOjos Posted July 12, 2010 Posted July 12, 2010 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 1
6Foot2 Posted July 12, 2010 Author Posted July 12, 2010 (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 July 12, 2010 by 6Foot2 Clarify post content.
LosOjos Posted July 12, 2010 Posted July 12, 2010 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 1
6Foot2 Posted July 12, 2010 Author Posted July 12, 2010 (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 July 12, 2010 by 6Foot2
LosOjos Posted July 12, 2010 Posted July 12, 2010 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 1
ba9ag Posted August 2, 2010 Posted August 2, 2010 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
6Foot2 Posted August 19, 2010 Author Posted August 19, 2010 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?
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now