egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Read a line from file script

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Script Requests
View previous topic :: View next topic  
Author Message
Football
Master


Joined: 26 Dec 2008
Posts: 205
Location: Quakenet, #Football

PostPosted: Sat Jun 09, 2012 11:40 am    Post subject: Read a line from file script Reply with quote

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.
Back to top
View user's profile Send private message
willyw
Revered One


Joined: 15 Jan 2009
Posts: 1175

PostPosted: Sat Jun 09, 2012 4:44 pm    Post subject: Re: Read a line from file script Reply with quote

Experiment with this:
Code:

# 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.
Back to top
View user's profile Send private message
Football
Master


Joined: 26 Dec 2008
Posts: 205
Location: Quakenet, #Football

PostPosted: Sun Jun 10, 2012 2:38 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
willyw
Revered One


Joined: 15 Jan 2009
Posts: 1175

PostPosted: Sun Jun 10, 2012 3:23 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
Football
Master


Joined: 26 Dec 2008
Posts: 205
Location: Quakenet, #Football

PostPosted: Sun Jun 10, 2012 3:30 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
willyw
Revered One


Joined: 15 Jan 2009
Posts: 1175

PostPosted: Sun Jun 10, 2012 10:46 am    Post subject: Reply with quote

Football wrote:
Yeah you're right, I apologize, its my fault.. I was thinking about something and wrote something else...


Razz

Quote:

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:


# 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"

}

Back to top
View user's profile Send private message
Football
Master


Joined: 26 Dec 2008
Posts: 205
Location: Quakenet, #Football

PostPosted: Sun Jun 10, 2012 11:24 am    Post subject: Reply with quote

Hey willyw,

You got it right... both times.. Smile

Thanks a lot, I apologize for earlier.
_________________
Idling at #Football, Quakenet.
Back to top
View user's profile Send private message
willyw
Revered One


Joined: 15 Jan 2009
Posts: 1175

PostPosted: Sun Jun 10, 2012 11:30 am    Post subject: Reply with quote

Football wrote:
Hey willyw,

You got it right... both times.. Smile

Thanks a lot,


You're welcome

Quote:

I apologize for earlier.


Smile It's ok. np
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Script Requests All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber