Version 2 of I need a tcl/tk developer to develop a small app for us

Updated 2011-04-13 20:27:25 by MHo

We are looking for some TCL/TK help.

We need a small app that will look through a folder of files, find every unique file extension, then create a folder for each extension and move the respective files into that folder.

I am trying to learn TCL/TK myself so I am looking to learn from your code so it should be heavily documented.

[email protected]


MHo 2011-04-13: Here's a quick try; not tested yet, not complete, just a first idea quickly hacked in. Sure there are errors in the code - perhaps someone with more time could assist, or show a different approach. And there is a good chance that I completely missinterpreted the task...

# (1)
# Loop over all files in the folder which is given as argument 1
#  assumption: no recursive search for now
#  Note: hidden files are ignored for now
#
foreach file [glob -dir [lindex $argv 0] -types f -tails] {
        # filling an array, using file extension as the index. Each element holds the names of all files of that type
        lappend buf([file extension $file]) $file
}
# (2)
# Now looping through all the extensions, and for each extension looping through all the files, doin' a little action
#  Note: no catching for errors until then...
#
foreach key [array names buf] {
        file mkdir folder_for_$key
        foreach file $buf($key) {
                file rename [file join [lindex $argv 0] $file] folder_for_$key           
        }
}