07-22-2017, 00:32 +0200
AuthorPost
CyBex  03-29-2004, 17:54   | [TCL] file Jabber  ICQ  AOL IM  Yahoo IM  MSN  Homepage
nie da
(Administrator)

Avatar

Member since 01/2004
260 Posts
Location: Berlin
Check, rename, delete and more with file(s)..
Quote by "TCL Manual":
file option <name> ?arg arg ...?

Manipulate file names and attributes

This command provides several operations on a file's name or attributes. Name is the name of a file; if it starts with a tilde, then tilde substitution is done before executing the command (see the manual entry for filename for details). Option indicates what to do with the file name. Any unique abbreviation for option is acceptable.

Manual: 8.3, 8.4, 8.5

See also: filenameopen, close, eof, gets, read, tell, seek, fblocked, flush

mfG CyBex @ QuakeNet, UnderNet, EFNet, Freenode, euIRC, GameSurge & ShadowWorld - Files
This post was edited 6 times, last on 12-28-2005, 14:04 by CyBex
CyBex  03-29-2004, 17:57   | use Jabber  ICQ  AOL IM  Yahoo IM  MSN  Homepage
nie da
(Administrator)

Avatar

Member since 01/2004
260 Posts
Location: Berlin
Usage:
FileCheck filename
FileCheck <filename>

Return:
1 if file exist else 0

Code:                                                                                                                               
01
02
03
04
05
06
07
08
09
10
proc FileCheck {FILENAME} {
    # check file exists
    if [file exists $FILENAME] then {
        # file exists
        return 1
    } else {
        # file not exists
        return "0"
    }
}


Example:                                                                                                                               
1
2
if {[FileCheck TESTFILE.TXT]} then { set text "File exists" } else { set text "File not exists" }
set result [FileCheck TESTFILE.TXT] ; # then 0/1 will bein $result


mfG CyBex @ QuakeNet, UnderNet, EFNet, Freenode, euIRC, GameSurge & ShadowWorld - Files
This post was edited 5 times, last on 12-28-2005, 14:08 by CyBex
CyBex  03-29-2004, 18:16   | read Jabber  ICQ  AOL IM  Yahoo IM  MSN  Homepage
nie da
(Administrator)

Avatar

Member since 01/2004
260 Posts
Location: Berlin
Usage:
FileTextRead filename ?linemode?
FileTextRead <filename> [linemode]

Return:
0 on error else DATA

Code:                                                                                                                               
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
proc FileTextRead {FILENAME {LINEMODE 0}} {
    # check exists and readable
    if {[file exists $FILENAME] && [file readable $FILENAME]} then {
        # open for readmode
        set FILE [open $FILENAME r]
        if {$LINEMODE!=1} then {
            # read buffer
            set READ [read -nonewline $FILE]
        } else {
            # read line
            set READ [get $FILE]
        }
        # release and return
        close $FILE
        return $READ
    }
    # not readable
    return 0
}

  • EN:Read FILENAME complete or only the first line (when LINEMODE is 1)
  • DE: Liest FILENAME entweder komplett oder nur die erste Zeile (wenn für LINEMODE eine 1 übergeben wird).
Other Code Example:                                                                                                                               
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
proc FileTextReadLine {FILENAME LINENR {METHODE 1}} {
    # starts with LINENR 0 = line1, 1=line2, ..., 199=line200, ..

    proc ReadWithEof {FILE LINENR} {
        set ReadNUM 0
        # not end of file reached? read nexline
        while ![eof $FILE] {
            set LINE [gets $FILE]
            if {$LINENR==$ReadNUM} {return $LINE}
            incr ReadNUM
        }
        # failed
        return 0
    }

    proc ReadFullAndSplit {FILE LINENR} {
        # read full file
        set BUFFER [read -nonewline $FILE]
        # convert to a list
        set LIST [split $BUFFER \n]
        # return Result
        return [lindex $LIST $LINENR]
    }

    # check file and parameter, return when failed
    if {![file exist $FILENAME] || ![file readable $FILENAME] || ![string is digit $LINENR]} {return 0}
    # open file
    set FILE [open $FILENAME r]
    if {$METHODE!=1} {
        # use first read method
        set LINE [ReadWithEof $FILE $LINENR]
    } {
        # use second (default) read method
        set LINE [ReadFullAndSplit $FILE $LINENR]
    }
    close $FILE
    return $LINE
}


How-to-Use Examples:                                                                                                                               
1
2
3
4
set text [FileTextRead TESTFILE.TXT]; foreach line [split $text \n] { puthelp "PRIVMSG #chan :$line" }
set firstline [FileTextRead TESTFILE.TXT 1]; # read the first line of TESTFILE.TXT
set firstline [FileTextReadLine "eggdrop.conf" 0 0]; # read the first line in eggdrop.conf with read/split.
set line [FileTextReadLine "eggdrop.conf" 50 0]; # read eggdrop.conf line 51 with the slower (but nicer with memory) gets/eof method.


mfG CyBex @ QuakeNet, UnderNet, EFNet, Freenode, euIRC, GameSurge & ShadowWorld - Files
This post was edited 13 times, last on 02-22-2007, 18:31 by CyBex
CyBex  03-29-2004, 20:32   | write Jabber  ICQ  AOL IM  Yahoo IM  MSN  Homepage
nie da
(Administrator)

Avatar

Member since 01/2004
260 Posts
Location: Berlin
Usage:
FileTextWrite filename data ?autoappend?
FileTextWrite <filename> <data> [autoappend]

Return:
1 when OK                                                                                                                               
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
proc FileTextWrite {FILENAME DATA {AUTOAPPEND 0} {NEWLINE 1}} {
    # when no file exists or not autoappend is on = create/overwrite
    if {![file exists $FILENAME] && $AUTOAPPEND!=1} then {
        # open for writemode
        set FILE [open $FILENAME w]
    } else {
        # open for appendmode
        set FILE [open $FILENAME a]
    }
    # write buffer
    if $NEWLINE {puts $FILE $DATA} {puts -nonewline $FILE $DATA}
    # release and return 1 for OK
    close $FILE
    return 1
}

  • EN:
  • DE: Legt FILENAME automatisch an oder fügt hinten an, wenn für AUTOAPPEND eine 1 übergeben wird. In dieser Datei wird dann DATA festgehalten.
  • OTHER:

mfG CyBex @ QuakeNet, UnderNet, EFNet, Freenode, euIRC, GameSurge & ShadowWorld - Files
This post was edited 8 times, last on 01-08-2006, 09:19 by CyBex
CyBex  03-29-2004, 20:33   | copy, move, rename, delete Jabber  ICQ  AOL IM  Yahoo IM  MSN  Homepage
nie da
(Administrator)

Avatar

Member since 01/2004
260 Posts
Location: Berlin
Usage:
FileBackup filename ?move?
FileBackup <filename> [move]

Return:
1 OK else 0                                                                                                                               
01
02
03
04
05
06
07
08
09
10
11
12
proc FileBackup {FILENAME {MOVE 0}} {
    if [file exists $FILENAME] {
        if [file exists $FILENAME.bak] {file delete $FILENAME.bak}
        if {$MOVE==1} {
            file rename  $FILENAME $FILENAME.bak
        } {
            file copy $FILENAME $FILENAME.bak
        }
        return 1
    }
    return 0
}


mfG CyBex @ QuakeNet, UnderNet, EFNet, Freenode, euIRC, GameSurge & ShadowWorld - Files
This post was edited 5 times, last on 03-29-2004, 22:06 by unknown
CyBex  03-29-2004, 20:33   | lines Jabber  ICQ  AOL IM  Yahoo IM  MSN  Homepage
nie da
(Administrator)

Avatar

Member since 01/2004
260 Posts
Location: Berlin
Usage:
 FileDeleteLine filename fromlinenr ?tolinenr? ?backup?
 FileDeleteLine <filename> <fromlinenr> [tolinenr] [backup]

Return:
1 if OK                                                                                                                               
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
proc FileDeleteLine {FILENAME LINENR {TOLINENR -1} {BACKUP 1}} {
    if {![file exist $FILENAME] || ![file readable $FILENAME]} {return 0}
    if {![string is digit $LINENR] || ![string is digit $TOLINENR]} {return 0}
    if {$TOLINENR==-1} {set TOLINENR $LINENR}
    # open file and read into a list
    set FILE [open $FILENAME r]
    set LIST [split [read -nonewline $FILE] \n]
    close $FILE
    # remove to lines from list
    set LIST [lreplace $LIST $LINENR $TOLINENR]
    # create (or not) a backup
    if {$BACKUP!=0} {
        # rename FILE (with force, because other oldbackup can exist)
        file rename -force  $FILENAME $FILENAME.bak
    }
    # overwrite file and puts file back
    set FILE [open $FILENAME w]
    puts -nonewline $FILE [join $LIST \n]
    close $FILE
    return 1
}

Other Example:
With Procs from upper explain about read and write.                                                                                                                               
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
proc FileDeleteOtherLine {FILENAME LINENR {TOLINENR -1} {BACKUP 1}} {
    if {![string is digit $LINENR] || ![string is digit $TOLINENR]} {return 0}
    set BUFFER [split [FileTextRead $FILENAME] \n]
    if {$BUFER!=0} {
        set BUFFER [lreplace $BUFFER $LINENR $TOLINENR]
        if {$BACKUP==1} {FileBackup $FILENAME 1}
        # write BUFFER without append and newline
        set RESULT [FileTextWrite $FILENAME [join $BUFFER \n] 0 0]
        if {$RESULT!=1} {
            # failed
            return 0
        }
        # ok
        return 1
    }
    # failed
    return 0
}


mfG CyBex @ QuakeNet, UnderNet, EFNet, Freenode, euIRC, GameSurge & ShadowWorld - Files
This post was edited 3 times, last on 01-22-2006, 15:12 by burn
CyBex  05-24-2005, 00:53   | Random line from a file Jabber  ICQ  AOL IM  Yahoo IM  MSN  Homepage
nie da
(Administrator)

Avatar

Member since 01/2004
260 Posts
Location: Berlin
Quote by "thommey":
                                                                                                                              
01
02
03
04
05
06
07
08
09
10
11
12
# Returns a random line out of $file (error if it doesnt exist)
proc randline {file} {
    set fs [open $file r]
    set data [read -nonewline $fs]
    close $fs
    # Now we have the complete content of $file stored in $data
    set data [split $data \n]
    # Now it's a list of lines, and we use eggdrop's "rand" function to get a random element from it (and return it)
    #return [lindex $data [rand [llength $data]]]
    # replaced by cybex with a Tcl rand
    return [lindex $data [expr {int(rand()*[llength $data])}]]
}


mfG CyBex @ QuakeNet, UnderNet, EFNet, Freenode, euIRC, GameSurge & ShadowWorld - Files
This post was edited 1 times, last on 01-08-2006, 09:21 by CyBex
CyBex  07-27-2007, 08:15   Jabber  ICQ  AOL IM  Yahoo IM  MSN  Homepage
nie da
(Administrator)

Avatar

Member since 01/2004
260 Posts
Location: Berlin
Quote by BoaR:
Could you please post a script to replace a line number from a file to with new text, the same way as the readline script ?
choosing the line number to delete and replace.

!replace
-- set line1 [replaceLine "$file" 0 0]
-- putserv "PRIVMSG $chan :replace line $line1"


                                                                                                                              
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
proc FileReplaceLine {FILENAME LINENR NEWTEXT {BACKUP 1}} {
    if {![file exist $FILENAME] || ![file readable $FILENAME]} {return 0}
    if {![string is digit $LINENR]} {return 0}
    # open file and read into a list
    set FILE [open $FILENAME r]
    set LIST [split [read -nonewline $FILE] \n]
    close $FILE
    # replace text from the list
    set LIST [lreplace $LIST $LINENR $LINENR $NEWTEXT]
    # create (or not) a backup
    if {$BACKUP!=0} {
        # rename FILE (with force, because other oldbackup can exist)
        file rename -force  $FILENAME $FILENAME.bak
    }
    # overwrite file and puts file back
    set FILE [open $FILENAME w]
    puts -nonewline $FILE [join $LIST \n]
    close $FILE
    return 1
}


mfG CyBex @ QuakeNet, UnderNet, EFNet, Freenode, euIRC, GameSurge & ShadowWorld - Files
This post was edited 1 times, last on 07-27-2007, 08:22 by CyBex
BoaR  07-28-2007, 15:20   ICQ
Member since 07/2007
6 Posts
thanks, it works
is there a way to write on a specific line when there's no text inside the file ?
I could FileTextWrite but it will just write on the first line too..
BoaR  07-28-2007, 15:32   ICQ
Member since 07/2007
6 Posts
Could you please post a script that will make the eggdrop to send a text on a channe 1 time a week, another eggdrop i dont own answers to the message and sends a file to my eggdrop, i want my eggdrop to reject the file but to check if the file name is different from another filename inside records.txt, if different, to write new file name inside records.txt delete old one. Records.txt will always have only 1 line.

thanks
This post was edited 1 times, last on 07-28-2007, 15:47 by BoaR
BoaR  07-28-2007, 15:44   ICQ
Member since 07/2007
6 Posts
oops * another post.
This post was edited 1 times, last on 07-29-2007, 11:36 by BoaR
Advanced options for this topic:

Ignore this topic (Do not list this topic in the "unread topics" search. You are currently not ignoring this topic.)
Hide this topic (Hidden topics are not displayed in the topics list. This topic is currently not hidden.)
Go to forum

Unclassified NewsBoard 1.5.3-d | © 2003-4 by Yves Goergen