Version 0 of Largest files

Updated 2008-07-02 10:38:11 by MB

MB: This is a simple mega-widget to display largest files than a given limit. It is based on snit, on the fileutil package and on the hrfilesize package, which can be found one the file size page. The user can customize :

  • the directory in which the search in done,
  • the file size limit,
  • the file size unit (B, KB, MB, etc...),
  • the file size limiter : min/max.

It should be able to handle large files over than 2 GB, but I did not check that. I use it to make some space on my hard drives.

    #
    # largestfilesview
    #   A mega-widget to display files larger/lesser than a given limit.
    #
    #   Copyright: Michael Baudin, 2008, [email protected]
    #
    package require snit
    package require hrfilesize
    package require Tk
    package require fileutil
    package require BWidget

    package provide largestfilesview 1.0

    snit::widget largestfilesview {
        variable wtext
        variable imageopen
        variable directoryname [pwd]
        variable sizelimit 1
        variable sizelimitunit MB
        variable sizeoperator min
        variable imagerun
        option -directoryentrywith -default 70
        constructor {} {
            set imageopen [image create photo -data {
                R0lGODlhGAAYAIYAAPwCBJTW7IzK7HS23Gyi1FSOxKTi9Jzi9JzW9ITG5GSi
                zDx2tCxirBRClKTi/BxOnAQ6jAxClER6vBxy5CRy5Hyu5HSu5AQibDSK1DyK
                1DSC1Gym5CyCzCR2zCxqzCRqxGSm3Iy67GSm5GSi3Byu7AQ2jFSe3BxyxEyW
                3ByW3ITS9ITS7AxqvESS1GTC7AQ6lFya1BSCzEyy7ESy5CR25EyS3BRmxCyi
                3Bx2zDSi5FSKzAxatBRavFx6tLzK5Ky+3LTG5MzW7Mza9NTi9OTq9PT2/FyO
                zLTC5LzO7PTy/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
                AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
                AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
                AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAAAAA
                LAAAAAAYABgAAAf/gACCg4MBAgMEBYSLjIIGBwgJAwoFCwwADY2LDgaMlg8Q
                moOdmgwPEaKpABKmqI0TFLGqmhQVFrcXs4sTFhgZGMAXwsPDmrwayMnKyrmM
                ExscHRweH9TUHx8T1djcoQATIB3iEyEiIiAb5yMiGyQHBvAlghMmJx0fFiYo
                Jvr8/ikqVqiQN8/EBxYeNqBogaIhw4YYUriY+GLQBBgs6KGI0QJZCmUtYsiY
                4U0QjRo2PpjokCxGSw0xYtwgQXBejQ81xOHosJPnBJ40WOSoKYiCjg87eOxY
                qnSpU6cVCU3Q0cPHDyBXfQQJImQIESJFikSVauTI1RBHfCBBIkTI17BFJYjO
                s4rVqg+tXL0SSSJX0IsSf/9CKDH4BYTAhHUpXsy4MSFwgQAAIf5oQ3JlYXRl
                ZCBieSBCTVBUb0dJRiBQcm8gdmVyc2lvbiAyLjUNCqkgRGV2ZWxDb3IgMTk5
                NywxOTk4LiBBbGwgcmlnaHRzIHJlc2VydmVkLg0KaHR0cDovL3d3dy5kZXZl
                bGNvci5jb20AOw==
            }]
            set imagerun [image create photo -data {
                R0lGODlhFgAWAIYAAPwCBFxWlDw6fExGhJyezDw2dFRSlHx6tISCtGRenFxe
                nGxqpHRurIR+tJSOxIyKvNTS7GRinDQ2dFRWlCwqZExGjGxupIyOvJSOvJSS
                xMzO5HRyrMTC5OTi9LS23KSezKSizLSu1DQydKyqzLSy1KSi1Kyq1Kyu1MS+
                5Ly63MzK7IyGvFxWnMS+3MzK5FROjExOjNza7ISCvGxmpMTC3Kym1GRmpHx2
                rMzG5NTO7IyKxExKjMTG5PTu/JyazOTe9Pz6/Ozm9Pz2/Pz+/JyaxPT2/Fxa
                nNzW7Ozu/Nza9AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
                AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
                AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
                AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAAAAA
                LAAAAAAWABYAAAf/gACCg4MBAoMDBISLiwUDBgIGAwcICYyMBQYKCwwHDQ4P
                EBGXABITFBUVEwoWCBcYGRoZBYwSAgMCGxwdEB4ODh8gIRmHi7ciIx0kGQ8X
                IB8lJicoEBbGAikqICsMLAICJCEhKS0uLi+DEjAIMRkyCgIigjMkKTQuEBAG
                ggUJLzE1HiQoJsiGPRUQYtD4EClChBsQMswgKGgDCoQuanx48M2ChQw4EFAE
                wIBGDgg4QOhgkMDUggMHcNyQN2gDjXwdQDywEU/Qjgc6ShwgyIAHzh4lGAhQ
                MAiGDh8fTAwAsMBmvh8dgLiIIIHQBKgmTHCwgANfjA5BhAyBAIMQkWDRWVKk
                4IAPaxEgQ3CYIBKAkBEOKjikwKHiSAwcJEx0cGEiww0KixZ8yAABXwcOG2/c
                yACCwQ6ahHBl6IAkicqJAgZSGJkOQAMXKD4o7Uvq0gRmA2vXttVVN51AACH+
                aENyZWF0ZWQgYnkgQk1QVG9HSUYgUHJvIHZlcnNpb24gMi41DQqpIERldmVs
                Q29yIDE5OTcsMTk5OC4gQWxsIHJpZ2h0cyByZXNlcnZlZC4NCmh0dHA6Ly93
                d3cuZGV2ZWxjb3IuY29tADs=
            }]

        }
        destructor {
            image delete $imageopen
            image delete $imagerun
        }
        #
        # draw --
        #   Draw the dialog
        #
        method draw {} {
            #
            # The toolbar contains buttons
            #
            set toolbar [frame $win.toolbar]
            pack $toolbar -anchor nw
            button $toolbar.diropen \
                -image $imageopen \
                -relief flat \
                -command [mymethod opendirectory]
            button $toolbar.runsearch \
                -image $imagerun \
                -relief flat \
                -command [mymethod searchfiles]
            pack $toolbar.diropen $toolbar.runsearch -anchor nw -side left
            #
            # Pack an entry to display the directory name
            #
            set directorybar [frame $win.directorybar]
            pack $directorybar -anchor nw
            label $directorybar.dirnamelabel \
                -text "Directory:"
            entry $directorybar.dirnameentry \
                -textvariable [myvar directoryname] \
                -width $options(-directoryentrywith)
            pack $directorybar.dirnamelabel $directorybar.dirnameentry -anchor nw -side left
            #
            # Pack widgets to set the (min,max), file size unit, and file size limit
            #
            set sizebar [frame $win.sizebar]
            pack $sizebar -anchor nw
            label $sizebar.sizelabel \
                -text "Size:"
            tk_optionMenu $sizebar.sizeunitom \
                [myvar sizelimitunit] \
                B KB MB GB TB EB
            tk_optionMenu $sizebar.sizeoperator \
                [myvar sizeoperator] \
                min max
            scale $sizebar.sizescale \
                -from 1 \
                -to 1000 \
                -variable [myvar sizelimit] \
                -orient horizontal \
                -showvalue 0 \
                -resolution 1
            entry $sizebar.sizeentry \
                -textvariable [myvar sizelimit]
            pack $sizebar.sizelabel $sizebar.sizeoperator \
                $sizebar.sizeunitom $sizebar.sizescale $sizebar.sizeentry -anchor nw -side left
            #
            # Pack a scrollable text to log messages
            #
            ScrolledWindow $win.scrolltext
            pack $win.scrolltext
            set wtext [text $win.thetext \
                -wrap none]
            $win.scrolltext setwidget $wtext
        }
        #
        # opendirectory --
        #   Let the user select the directory to explore
        #
        method opendirectory {} {
            set dirname [tk_chooseDirectory \
                             -initialdir $directoryname]
            if {$dirname!=""} then {
                set directoryname $dirname
            }
        }
        #
        # searchfiles --
        #   Search the files matching the criteria.
        #
        method searchfiles {} {
            $self log "Searching files in $directoryname..."
            #
            # Compute the list of files
            #
            set listoffiles [::fileutil::find $directoryname [mymethod filterfile]]
            set nbfiles [llength $listoffiles]
            $self log "Number of files over the minimum : $nbfiles"
            #
            # Sort files by size
            #
            set datatosort {}
            foreach filename $listoffiles {
                set size [file size $filename]
                set data [list $filename $size]
                lappend datatosort $data
            }
            set sorteddata [lsort -decreasing -index 1 -integer $datatosort]
            #
            # Display the sorted files matching the filter
            #
            foreach data $sorteddata {
                set filename [lindex $data 0]
                set size [lindex $data 1]
                set hrsize [hrfilesize::bytestohr $size]
                $self log "$hrsize \t $filename "
            }

        }
        #
        # filterfile --
        #   Returns 1 if the file is to keep in the list or 1 if the file is to
        #   exclude from the list.
        #
        method filterfile {filename} {
            set isfile [file isfile $filename]
            if {$isfile==0} then {
                set filter 0
            } else {
                set size [file size $filename]
                set hrfilesize [hrfilesize::bytestohr $size]
                set hrsizelimit "$sizelimit $sizelimitunit"
                set compare [hrfilesize::compare $hrfilesize $hrsizelimit]
                switch -- $sizeoperator {
                    "min" {
                        set filter [expr {$compare>=0}]
                    }
                    "max" {
                        set filter [expr {$compare<=0}]
                    }
                    default {
                        error "Unknown size operator $sizeoperator"
                    }
                }
            }
            return $filter
        }
        #
        # log --
        #   Display the given message on the text widget
        #
        method log {msg} {
            $wtext insert end "$msg\n"
            $wtext see end
        }

    }

An here is a simple client.

    #
    # largestfilesview-gui
    #   A sample testcase for the largestfilesview mega-widget.
    #
    lappend ::auto_path hrfilesize-1.1
    lappend ::auto_path largestfilesview-1.0

    package require largestfilesview

    set dialog [largestfilesview create .mydialog]
    pack $dialog
    $dialog draw

enter categories here