cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
3607
Views
0
Helpful
7
Replies

TCL script to check the number of file in directory before write the output for ASR9K

Rojer-bkk
Level 1
Level 1

Hi Expert,

Can anyone help me shed some light, how can i create the TCL to checking the number of file in directory before writing the new file in directory?  I need to limit the amount of file store in directory. Platform ASR9K. Thanks in advance

2 Accepted Solutions

Accepted Solutions

Here is the specific Tcl code that will work.

set files [glob -directory "harddisk:/BGP-script" -- *]

if { [llength $files] == 10 } {

    set oldest [list [lindex $files 0] [file mtime [lindex $files 0]]]

    foreach file $files {

        set mtime [file mtime $file]

        if { $mtime < [lindex $oldest 1] } {

            set oldest [list $file $mtime]

        }

    }

    file delete -force [lindex $oldest 0]

}

View solution in original post

The algorithm is incomplete.  The code still just finds the oldest.  Might be better to use the built-in Tcl for this:

if { [llength $files] >= 5 } {
    array set oldest [list]
    foreach file $files {
        set oldest([file mtime $file]) $file
    }
    set mtimes [lsort -integer [array names oldest]]
    for {set i 0} {$i < 2} {incr i} {
        file delete -force $oldest([lindex $mtimes $i])
    }
}

   

View solution in original post

7 Replies 7

Joe Clarke
Cisco Employee
Cisco Employee

Post the output of the dir command from the ASR9K.  Are you looking for a specific filename pattern or just the total number of all files on disk?

Hi Joe,

The object is I prefer to limit only have 10 files in directory and oldest file would be deleted if file no.11 is written into directory. Thanks for your advise.

RP/0/RSP0/CPU0:ios#dir harddisk:/BGP-script 

Directory of harddisk:/BGP-script

 25264       -rw-  351119      Mon Aug  8 14:56:24 2016  script-log-20160808145254.txt
25262       -rw-  351905      Mon Aug  8 15:03:55 2016  script-log-20160808150025.txt
25261       -rw-  353066      Mon Aug  8 15:14:52 2016  script-log-20160808151121.txt
290336      -rw-  354160      Mon Aug  8 15:22:36 2016  script-log-20160808151905.txt
290337      -rw-  35739       Mon Aug  8 15:22:34 2016  show_tech_bgp_20160808151905.tgz

6576652288 bytes total (6511952896 bytes free)

Here is the specific Tcl code that will work.

set files [glob -directory "harddisk:/BGP-script" -- *]

if { [llength $files] == 10 } {

    set oldest [list [lindex $files 0] [file mtime [lindex $files 0]]]

    foreach file $files {

        set mtime [file mtime $file]

        if { $mtime < [lindex $oldest 1] } {

            set oldest [list $file $mtime]

        }

    }

    file delete -force [lindex $oldest 0]

}

Hi Joe,

Thank you for script. It is working as expected. Lastly, if i need to delete more old files such as two oldest files then the script below should be working?

set files [glob -directory "harddisk:/BGP-script/" -- *]
  if { [llength $files] >= 5 } {
  set oldest [list [lindex $files 0 && lindex $files 1 ] [file mtime [lindex $files 1]]]
  foreach file $files {
        set mtime [file mtime $file]
        if { $mtime < [lindex $oldest 2] } {
             set oldest [list $file $mtime]
        }
    }

No.  You'll need to maintain a list of the files in order of oldest to newest.

set files [glob -directory "harddisk:/BGP-script" -- *]

if { [llength $files] == 10 } {

    set oldest [list [lindex $files 0]]

    set mtime [file mtime [lindex $files 0]]

    foreach file $files {

        set mt [file mtime $file]

        if { $mt < $mtime } {

            set oldest [linsert $oldest 0 $file]

            set mtime $mt

        }

    }

    for {set i 0} {$i < 2} {incr i} {

        file delete -force [lindex $oldest $i]

    }

}

Hi Joe,
Thanks for script. I have tested and there was something issue. Please consider here

1st :: Original file in harddisk:/BGP-script/

25268 -rw- 156078 Tue Aug 16 18:54:07 2016 script-log-20160816185118.txt
290336 -rw- 45644 Tue Aug 16 18:54:05 2016 show_tech_bgp_20160816185118.tgz
25262 -rw- 65536 Tue Aug 16 19:29:24 2016 script-log-20160217195612.txt
25261 -rw- 73886 Tue Aug 16 19:29:25 2016 script-log-20160713195933.txt
290322 -rw- 27002 Tue Aug 16 19:29:26 2016 script-log-19800104073234.txt

2nd:: After script run, the correct two oldest files are deleted as expected

290336 -rw- 185471 Tue Aug 16 19:49:55 2016 script-log-20160816194659.txt
25268 -rw- 66052 Tue Aug 16 19:49:52 2016 show_tech_bgp_20160816194659.tgz
25262 -rw- 65536 Tue Aug 16 19:29:24 2016 script-log-20160217195612.txt
25261 -rw- 73886 Tue Aug 16 19:29:25 2016 script-log-20160713195933.txt
290322 -rw- 27002 Tue Aug 16 19:29:26 2016 script-log-19800104073234.txt

3rd:: Actually, the two oldest files with time stamp 19:29:24 and 19:29:25 should be deleted but the script deleted the  file 19:29:24 and 19:49:52. 


290336 -rw- 185471 Tue Aug 16 19:49:55 2016 script-log-20160816194659.txt
25268 drwx 4096 Tue Aug 16 19:52:27 2016 show_tech_bgp_20160816195139 
25262 -rw- 188416 Tue Aug 16 19:52:26 2016 script-log-20160816195139.txt
25261 -rw- 73886 Tue Aug 16 19:29:25 2016 script-log-20160713195933.txt
290322 -rw- 27002 Tue Aug 16 19:29:26 2016 script-log-19800104073234.txt

==== Script =====

set files [glob -directory "harddisk:/BGP-script" -- *]
   if { [llength $files] >= 5 } {
      set oldest [list [lindex $files 0]]
      set mtime [file mtime [lindex $files 0]]
    foreach file $files {
       set mt [file mtime $file]
    if { $mt < $mtime } {
    set oldest [linsert $oldest 0 $file]
    set mtime $mt
    }
  for {set i 0} {$i < 2} {incr i} {
     file delete -force [lindex $oldest $i]
        set filename [format "/harddisk:/BGP-script/script-log-%s.txt" $tdate]
        set outfile [open $filename w]
        action_syslog priority info msg "Opened $filename"
        puts $outfile "The syslog that trigger the EEM script is:"
        puts $outfile $syslog_msg
     }
}

The algorithm is incomplete.  The code still just finds the oldest.  Might be better to use the built-in Tcl for this:

if { [llength $files] >= 5 } {
    array set oldest [list]
    foreach file $files {
        set oldest([file mtime $file]) $file
    }
    set mtimes [lsort -integer [array names oldest]]
    for {set i 0} {$i < 2} {incr i} {
        file delete -force $oldest([lindex $mtimes $i])
    }
}

   

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: