:TITLE: Changing Working Directory - cd, pwd
;#
;# RCSID: $Header: /home/cvs/TclTutor3/lesson/Tcl_English/Tcl_English35.lsn,v 1.1 2008/08/02 12:37:17 clif Exp $
;# Copyright (c) 1995 Clif Flynt
;# 9300 Fleming Rd.
;# Dexter, MI  48130
;# clif@cflynt.com
;# See file "NOTICE" for licensing terms.
;#
::CMD:: if {([info exists tcl_platform])} {
  switch $tcl_platform(platform) {
    unix        {
                set Tutor(lsn.codeMod) {
                    {regsub TEMPDIR $line "/bin /usr/bin /usr/local/bin]" line}
                }
        }
    windows     {
                set Tutor(lsn.codeMod) {
                    {regsub TEMPDIR $line "C:/windows C:/windows/system C:/temp C:/foo " line}
                }         
        }
    mac         {
                set Tutor(lsn.codeMod) {
                    {regsub TCL_INTERP $line "$tutorGlobals(bindir):$tutorGlobals(interpreter)" line}
		    {regsub "wish" $line "tclsh" line}
		}
                }
    default     {
                puts "I don't recognize the platform: $tcl_platform(platform)"
                puts "Can't set platform specific parameters"
                }
    } 
  }

:LESSON_TEXT_START_LEVEL 0:
Tcl also supports commands to change and display the current working
directory. 
<P>
These are:
<DL>
<B><DT>cd</B> <I>?dirName?</I>
<DD>Changes the current directory to <I>dirName</I> (if <I>dirName</I> is
given, or to the <B>$HOME</B> directory if <I>dirName</I> is not given.
If <I>dirname</I> is a tilde (<B>~</B>, <CODE>cd</CODE> changes the working 
directory to the users home directory.  If <I>dirName</I> starts with a
tilde, then the rest of the characters are treated as a login id, and 
<CODE>cd</CODE> changes the working directory to that user's $HOME.
<B><DT>pwd</B>
<DD>Returns the current directory.
</DL>
:TEXT_END:

:LESSON_TEXT_START_LEVEL 1:
Tcl also supports commands to change and display the current working
directory. 
<P>
These are:
<DL>
<B><DT>cd</B> <I>?dirName?</I>
<DD>Changes the current directory to <I>dirName</I> (if <I>dirName</I> is
given, or to the <B>$HOME</B> directory if <I>dirName</I> is not given.
If <I>dirname</I> is a tilde (<B>~</B>, <CODE>cd</CODE> changes the working 
directory to the users home directory.  If <I>dirName</I> starts with a
tilde, then the rest of the characters are treated as a login id, and 
<CODE>cd</CODE> changes the working directory to that user's $HOME.
<B><DT>pwd</B>
<DD>Returns the current directory.
</DL>
:TEXT_END:

:LESSON_TEXT_START_LEVEL 2:
Most modern operating systems support a tree based directory structure,
one in which a given directory may have numerous subdirectories, which
may also have subdirectories.  
<P>
The path to a given file or subdirectory can be arbitrarily long, and
can become difficult to work with.  Thus, it is convenient to change the
current working directory to one that contains the files the program
is accessing.
<P>
Tcl supports commands to change and display the current
working directory. 
<P>
These are:
<DL>
<B><DT>cd</B> <I>?dirName?</I>
<DD>Changes the current directory to <I>dirName</I> (if <I>dirName</I> is
given, or to the <B>$HOME</B> directory if <I>dirName</I> is not given.
If <I>dirname</I> is a tilde (<B>~</B>, <CODE>cd</CODE> changes the working 
directory to the users home directory.  If <I>dirName</I> starts with a
tilde, then the rest of the characters are treated as a login id, and 
<CODE>cd</CODE> changes the working directory to that user's $HOME.
<B><DT>pwd</B>
<DD>Returns the current directory.
</DL>
<P>
The example code could also be written as:
<PRE>
foreach dir $dirs {
  set c_files [glob $dir/c*]
  
  foreach name $c_files {
    puts "[format "%-15s  %-20s " [file tail $name] $dir ]"
    }
  }
</PRE>
Adding the directory name to the <CODE>glob</CODE> command also adds that 
directory path to each name in the list.  Depending on your program you may
want or not want that information carried with each name.
:TEXT_END:

:CODE_START:
set dirs [list TEMPDIR]


puts "[format "%-15s  %-20s " "FILE" "DIRECTORY"]"

foreach dir $dirs {
  catch {cd $dir}
  set c_files [glob -nocomplain c*]

  foreach name $c_files {
    puts "[format "%-15s  %-20s " $name [pwd]]"
    }
  }

:TEXT_END:

