1. The 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.

    Syntax: cd dirName
    Changes the current working directory to dirName.

    Syntax: pwd
    Returns the current working directory.

    Solution

  2. The 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.

    Syntax: glob ?-nocomplain? pattern
    The 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

  3. Solution

  4. The 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
    

    Syntax: file extension path
    Returns the portion of path after the last extension marker.

    Solution

  5. Use the 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:

    Solution

  6. It's useful to know whether something is a file or a folder.

    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:

    Solution

  7. Rework the bulk of this script to be a procedure named showFolder that accepts the name of a directory to display as an argument.

    When you run this, it will look the same as before.

    Solution

  8. Here's a bit of magic to add to the showFolder procedure to destroy all the windows.

    
    destroy {*}[winfo children .]
    
    1. Add destroy {*}[winfo children .] to the showFolder procedure.
    2. In the 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

  9. The previous application will step down into a folder, but it can't step back up.

    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 .."

    Solution


Copyright Clif Flynt 2010