This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Read a line from file script

Requests for complete scripts or modifications/fixes for scripts you didn't write. Response not guaranteed, and no thread bumping!
Post Reply
F
Football
Master
Posts: 205
Joined: Fri Dec 26, 2008 3:08 pm
Location: Quakenet, #Football

Read a line from file script

Post by Football »

Hi all!

Was wondering if someone could help me out.

I need a script that will check every X minutes (determined in the script) if there are any lines written in a file called 'Stats.txt', if there is - it will write the first line in the file to channel(s) set in the .tcl file and erase that file.

If there are no lines in the file - it won't do anything, just keeping on checking the file and read/erase once there are lines in Stats.txt

Thanks
Idling at #Football, Quakenet.
w
willyw
Revered One
Posts: 1200
Joined: Thu Jan 15, 2009 12:55 am

Re: Read a line from file script

Post by willyw »

Experiment with this:

Code: Select all

# June 9, 2012
# http://forum.egghelp.org/viewtopic.php?t=19001
#

# set how often file check runs - in minutes
set sched 1

# set path and filename of stats file
set statsfile "scripts/added/testing/Stats.txt"

# set channels in which to announce
set announcechans "#chan1 #chan2"

##
timer $sched readthefile

proc readthefile { } {
global sched statsfile announcechans
	if {![file exists $statsfile]} {
		timer $sched readthefile		
		return 0
         }
	# reference:  http://forum.egghelp.org/viewtopic.php?t=6885
	set fname $statsfile
	set fp [open $fname "r"]
	set data [read -nonewline $fp]
	close $fp
	set lines [split $data "\n"] 
	if {[lindex $lines 0] == ""} {
		timer $sched readthefile
           return 0
        }
	foreach channel $announcechans {
		if {[botonchan $channel]} {
			putserv "privmsg $channel :[lindex $lines 0]"
		    }
	  }
	file delete $statsfile
	timer $sched readthefile
}
	
bind evnt - prerehash cleartimers

proc cleartimers {prerehash} {
	foreach timerlisted [timers] {
		if {[lindex $timerlisted 1] == "readthefile" } {
			killtimer [lindex $timerlisted 2]
              }
	   }
}
Tested briefly, and it seemed to do what you described. I hope this helps.
F
Football
Master
Posts: 205
Joined: Fri Dec 26, 2008 3:08 pm
Location: Quakenet, #Football

Post by Football »

Hey willyw,

Thanks for the code

However it seems that the script reads the first line from Stats.txt and then erases the whole txt, which isn't good.

* Also wondering if you can please add a public command for adding lines to Stats.txt please
Idling at #Football, Quakenet.
w
willyw
Revered One
Posts: 1200
Joined: Thu Jan 15, 2009 12:55 am

Post by willyw »

Football wrote:
However it seems that the script reads the first line from Stats.txt and then erases the whole txt, which isn't good.
[...]
Football wrote: ... it will write the first line in the file to channel(s) set in the .tcl file and erase that file.
?
That's how I read your request.
F
Football
Master
Posts: 205
Joined: Fri Dec 26, 2008 3:08 pm
Location: Quakenet, #Football

Post by Football »

Yeah you're right, I apologize, its my fault.. I was thinking about something and wrote something else... The script is meant to read each X minutes the next line in the file. Each time it reads a line, it erases that line from the file..
Idling at #Football, Quakenet.
w
willyw
Revered One
Posts: 1200
Joined: Thu Jan 15, 2009 12:55 am

Post by willyw »

Football wrote:Yeah you're right, I apologize, its my fault.. I was thinking about something and wrote something else...
:P
The script is meant to read each X minutes the next line in the file. Each time it reads a line, it erases that line from the file..

Try this:

Code: Select all


# June 10, 2012

# http://forum.egghelp.org/viewtopic.php?t=19001




#######



# Usage: To add a line, do:        !addline <text of line here>        in channel.
#        To see all lines, do:  !sayfile




# set how often file check runs - in minutes
set sched 1

# set path and filename of stats file
set statsfile "scripts/added/testing/Stats.txt"

# set channels in which to announce
set announcechans "#channel1 #channel2"




bind pub - "!addline" addline
bind evnt - prerehash cleartimers
bind pub - "!sayfile" sayfile


timer $sched readthefile

proc readthefile { } {
global sched statsfile announcechans


	if {![file exists $statsfile]} {
		timer $sched readthefile		
		return 0
         }


	# reference:  http://forum.egghelp.org/viewtopic.php?t=6885
	set fname $statsfile
	set fp [open $fname "r"]
	set data [read -nonewline $fp]
	close $fp

	set lines [split $data "\n"] 


	if {[lindex $lines 0] == ""} {
		timer $sched readthefile
           return 0
        }

	foreach channel $announcechans {
		if {[botonchan $channel]} {
			putserv "privmsg $channel :[lindex $lines 0]"
		    }
	  }


	
	set fname $statsfile
	set fp [open $fname "r"]
	set data [read -nonewline $fp]
	close $fp

	set lines [split $data "\n"] 


	# reference:  http://forum.egghelp.org/viewtopic.php?t=6885
	set line_to_delete 0
	set lines [lreplace $lines $line_to_delete $line_to_delete]
	set fp [open $fname "w"]
	puts $fp [join $lines "\n"]
	close $fp 



	timer $sched readthefile

}
	

###

proc cleartimers {prerehash} {

	foreach timerlisted [timers] {
		if {[lindex $timerlisted 1] == "readthefile" } {
			killtimer [lindex $timerlisted 2]
              }
	   }
}


###



proc addline {nick uhost handle chan text} {
global statsfile

	if {$text == ""} {
		return 0
         }

	# reference:   http://forum.egghelp.org/viewtopic.php?t=6885
	set line_to_add $text
	set fname $statsfile
	set fp [open $fname "a"]
	puts $fp $line_to_add
	close $fp 
}


###

proc sayfile {nick uhost handle chan text} {
global statsfile

	if {![file exists $statsfile]} {
	putserv "privmsg $chan :File does not exist"
		return 0
         }


	set fname $statsfile
	set fp [open $fname "r"]
	set data [read -nonewline $fp]
	close $fp

	set lines [split $data "\n"]


	putserv "privmsg $chan :$lines"

}

F
Football
Master
Posts: 205
Joined: Fri Dec 26, 2008 3:08 pm
Location: Quakenet, #Football

Post by Football »

Hey willyw,

You got it right... both times.. :-)

Thanks a lot, I apologize for earlier.
Idling at #Football, Quakenet.
w
willyw
Revered One
Posts: 1200
Joined: Thu Jan 15, 2009 12:55 am

Post by willyw »

Football wrote:Hey willyw,

You got it right... both times.. :-)

Thanks a lot,
You're welcome
I apologize for earlier.
:) It's ok. np
Post Reply