pwd
command will return the current
working directory for an application. The cd
command will change an application's
working directory to a new location.
Write a script that starts in one directory and then changes to another and prints the new location.
If you are working on a Windows system, remember that within Tcl all file paths use forward slashes, not backwards slashes.
Add a directory that doesn't exist as the last folder to cd to. Note the error message.
cd dirName
dirName
.
pwd
glob
command will return the contents of a
folder as a Tcl list. You can add a glob
style argument
to select which of the contents to show.
Modify the previous code to show the contents in the folder, then modify the script again to show only some of the contents.
glob ?-nocomplain? pattern
glob
command returns a list of file names that
match a pattern.
-nocomplain
| Do not throw an error if no files match the pattern |
pattern
| A glob style pattern to match |
file extension
command will return the extension
of a file - the part after the last period.
MS Windows uses this extension to determine what kind of data is in a file.
We can build our own extension-to-file-type application with a keyed list using code like this:
source keyedListLibByReference.tcl
set idList {}
foreach k {.tcl .exe .txt .htm .html} \
v {{Tcl File} {Windows Executable} {Text} {HTML} {HTML}} {
appendKeyedPair idList $k $v
}
Download the keyedListLibByReference.tcl
file to be merged into your application with the source
command.
Write an application that will cd to a directory (/tmp
, your
home directory, or perhaps /Volumes/Scratch
) and use a
foreach
loop to look at each file, extract the extension
and puts
a description of files it recognizes.
The output should look something (but not exactly) like this:
makeHTML2_1_1.exe is a Windows Executable file
balloon.tcl is a Tcl File file
TS-AudioToMIDI.exe is a Windows Executable file
nwa-4-2009.html is a HTML file
file extension path
path
after the last
extension marker.
glob
command to show the elements in
a folder in a GUI. Use a label to show each thing in the folder.
The results should look something like this:
The file type
command will return what type of entity
is in a folder - it will return file, directory,
characterSpecial, blockSpecial, fifo, link,
or
socket
. Note that this is what the entity is, not
what it contains.
The label command has options we didn't discuss in class. One of them is
the -foreground
option which will define the color of the
text. The color can be the name of any common color (red, orange,
yellow, green, blue, purple, cyan, magenta, violet, gray, brown, etc)
This command will make a label with blue text, instead of the normal black.
set w [label .l[incr winNum] -text $name -foreground blue]
Rework the previous example color code the labels to show whether a directory entity is a file or folder.
The results might look like this:
showFolder
that accepts the
name of a directory to display as an argument.
When you run this, it will look the same as before.
showFolder
procedure
to destroy all the windows.
destroy {*}[winfo children .]
destroy {*}[winfo children .]
to the showFolder
procedure.
if
statement inside the showFolder
procedure,
change the label to a button, and make the command showFolder $name
The button options you'll need are:
-text text string
| The text to display in the button |
-command script
| The script to evaluate when the button is activated. |
-foreground color
| The color to use for this text |
This is now a simple file browser that will go into folders. Solution
Unix, Linux, Mac OS X and Windows all use a convention that each
folder has a hidden folder named "..
". The "..
"
folder is the current folder's parent.
Modify the showFolder
procedure so that before
the foreach
loop it creates and grids a button that says
"Up". The command for this will be "showFolder .."