This would be fully possible, indeed.
Most of the things you'll need is available through the "file" and "glob" commands.
Breaking it down into the various steps:
Getting a list of directories in the source directory
Code: Select all
set Directories [glob -type d /source/*]
This will retrieve all directories under /root. You might consider filtering out directories with one-letter names if your source and destination trees are the same, otherwize you'd just try to move the /root/T directory to be located under /root/T (being /root/T/T, then /root/T/T/T, etc..).
Simplest way to achieve this, is to modify the glob-pattern to something like /source/??*
Extracting the first letter of the directory to be moved
Code: Select all
set Target "/destination/[string index $Source 0]"
This assumes that the name of the directory to be moved, has been stored in Source.
If combining this with the glob-code above, you'd end up with something like this:
Code: Select all
foreach Source [glob -type d /source/*] {
set Target "/destination/[string index $Source 0]"
#...
}
If you'd like to make sure that the destination is always upper-case, you could modify the code to use "string toupper":
Code: Select all
set Target "/destination/[string toupper [string index $Source 0]]"
Moving the directory
First off, we'll have to make sure that the destination directory already exists. Otherwize, we'd have to create it before doing anything else
Code: Select all
if {![file exists $Target]} {
file mkdir $Target
} elseif {![file isdirectory $Target]} {
error "$Target already exists as a file"
}
The last part is there to handle the case when there might already be a file with the same name as the target directory.
Next, do the actual move