Nyquist / XLISP 2.0 - Contents | Tutorials | Examples | Reference

# Files and Directories

- Interactive Functions

- pwd - returns the current working directory
- cd - changes the current working directory

- Testing Files and Directories

- directory-exists-p - tests if a directory exists
- file-exists-p - tests if a file exists
- filename-exists-p - tests if a file or directory exists

- Testing Filenames

- absolute-filename-p - tests if a string is an absolute filename

- System Environment Variables

- windows-p - tests if the operation system is a Windows system
- user-home-directory - returns path to the user's HOME directory
- expand-tilde - replaces "~/" with the user's HOME directory

## pwd

The 'pwd' function returns the current working directory:

    (defun pwd ()
      (setdir "."))

Ok, this function does not belong to the masterpieces of computer science, but (pwd) is much easier to remember than (setdir ".").

Back to top

## cd

The 'cd' function changes the current working directory. The directory name must be given as a string:

    (defun cd (string)
      (cond ((not (stringp string))
             (error "argument must be a string" string))
            ((string= "." string)
             (setdir "."))
            (t
             (let ((orig-dir (setdir "."))
                   (new-dir  (setdir string)))
               (when (string/= orig-dir new-dir)
                 new-dir)))))

Possible actions and return values are:

- It the argument is not a string, then an error will be raised.

- If the directory name is ".", then the name of the current working directory is returned as a string. This is the same effect as if the directory has been changed to itself.

- If the directory has successfully been changed to the given directory, then the name of the new working directory is returned as a string.

- If the given directory has not been found, then NIL [= false] is returned.

Back to top

## directory-exists-p

The 'directory-exists-p' function tests if a directory exists. The directory name must be given as a string:

    (defun directory-exists-p (string)
      (cond ((not (stringp string))
             (error "argument must be a string" string))
            ((string= "." string)
             (setdir "."))
            (t
             (let ((orig-dir (setdir "."))
                   (new-dir  (setdir string)))
               (when (string/= orig-dir new-dir)
                 (setdir orig-dir)
                 new-dir)))))

Possible actions and return values are:

- It the argument is not a string, then an error will be raised.

- If the directory name is ".", then the absolute name of the current working directory is returned as a string. This is not a very useful test, but makes the return values consistent.

- If the directory has been found, then the absolute name of the directory is returned as a string.

- If the directory has not been found, then NIL [= false] is returned.

The 'directory-exists-p' function is nearly the same as the cd function above. The only difference is that the working directory will automatically be changed back to the initial directory.

On Unix, with soft-links, the absolute name of the target directory [i.e. not the name of the link-file itself, but the name of the directory the link points to] is returned.

Implementation Notes

The Nyquist 'setdir' function always returns absolute directory names, even if a relative directory name has been given as a string by the user. That's why it's not possible to reliably compare the return value of (setdir string) directly with 'string'. Instead the absolute name of the initial working directory, returned by (setdir "."), is compared to the absolute name, returned when (setdir string) tries to change the directory. If both return values are the same, then (setdir string) has failed because the directory has not been found.

If the directory string is ".", then this trick doesn't work, because the initial directory is the same as the target directory, so even if the directory has 'successfully' been changed to itself, both return values still would be the same. This is one of the reasons why "." has a separate 'cond' clause. The other reason is of course that it makes not really much sense to change a directory to itself, that's why we save the work and just return the absolute name of the current working directory.

Back to top

## file-exists-p

The 'file-exists-p' function tests if a file exists. The file name must be given as a string:

    (defun file-exists-p (string)
      (if (not (stringp string))
          (error "argument must be a string" string)
          (unless (directory-exists-p string)
            (let (file-stream)
              (unwind-protect
                (setq file-stream (open string))
                (when file-stream (close file-stream)))
              (when file-stream string)))))

On Unix systems a directory is a special kind of file, so on Unix the XLisp 'open' function can open directories, too. That's why we first must make sure that no directory exists with the same name as the file that we are looking for.

Back to top

## filename-exists-p

    (defun filename-exists-p (string)
      (if (not (stringp string))
          (error "argument must be a string" string)
          (or (directory-exists-p string)
              (file-exists-p string)))

Back to top

## absolute-filename-p

The 'absolute-filename-p' function tests if a string is an absolute file or directory name:

    (defun absolute-filename-p (string)
      (if (not (stringp string))
          (error "argument must be a string" string)
          (let ((end (length string)))
            (when (or (and (>= end 1)  ; Unix "/..."
                           (char= #\/ (char string 0)))
                      (and (>= end 3)  ; Windows "[a-zA-Z]:[\/]..."
                           (let ((char (char string 0)))
                             ;; upper- or lowercase character a-z, A-Z
                             (and (> (char-code (char-downcase char)) 96)
                                  (< (char-code (char-downcase char)) 123)))
                           (char= #\: (char string 1))
                           (let ((char (char string 2)))
                             (or (char= #\\ char)
                                 (char= #\/ char)))))
              string))))

Note that it is only tested whether the beginning of the string matches the beginning of an absolute file or directory name. It is not tested whether the string reperesents a meaningful name.

Back to top

## System Environment Variables

## get-env

[This function works only with Audacity 1.3.13 and above.]

(get-env "environment-variable")

Back to top

## windows-p

[This function works only with Audacity 1.3.13 and above.]

The 'windows-p' function tests if the underlying operation system is a Microsoft Windows[tm] system:

    (defun windows-p ()
      (let* ((home (let ((drive (get-env "HOMEDRIVE"))
                         (path  (get-env "HOMEPATH")))
                     (if (and drive path)
                         (strcat drive path)
                         (get-env "UserProfile"))))
             (path (get-env "PATH")))
        (when home  ; if HOMEDRIVE + HOMEPATH or UserProfile exist
          (if path  ; search for Windows :\ drive-letter patterns
              (string-search ":\\" path)
              (error "no PATH environment variable found")))))

Nquist has a *file-separator* variable, that could be used much easier to detect the operation system:

    (defun windows-p ()
      (char= *file-separator* #\\))

Back to top

## user-home-directory

[This function works only with Audacity 1.3.13 and above.]

The 'user-home-directory' function returns the path to the user's home directory on Linux, Mac, and Windows:

    (defun user-home-directory ()
      (or (get-env "HOME")
          (let ((drive (get-env "HOMEDRIVE"))
                (path  (get-env "HOMEPATH")))
            (when (and drive path)
              (strcat drive path)))
          (get-env "UserProfile")))

If the user's home directory could be identified, then the path to the home directory is returned as a string. If the user's home directory could not be identified, then NIL [= false] is returned.

Examples:

    (user-home-directory)  => "/home/edgar"                        ; Linux
    (user-home-directory)  => "C:\\Documents and Settings\\Edgar"  ; Windows

On Windows there is no HOME variable defined by Windows itself, but most programs will respect a HOME variable, if one had been defined by the user. This means that on Windows, if a HOME variable exists, the HOME variable will be used instead of HOMEDRIVE and HOMEPATH or 'UserProfile'.

Back to top

## expand-tilde

[This function works only with Audacity 1.3.13 and above.]

    (defun expand-filename (string)
      (cond ((not (stringp string))
             (error "argument must be a string" string))
            ((and (> (length string) 1)
                     (char= #\~ (char string 0))
                     (or (char= #\/ (char string 1))
                         (char= #\\ (char string 1))))
             (strcat (user-home-directory)
                     (subseq string 1)))
            (t string)))

Back to top

Nyquist / XLISP 2.0 - Contents | Tutorials | Examples | Reference