directory-depth

Name

directory-depth -- Count the directory depth of a path name

Synopsis

(directory-depth pathname)

Description

Returns the number of directory levels in pathname

The pathname must end in a filename. Further, this function assumes that directories in a pathname are separated by forward slashes ("/").

Example

"filename" => 0, "foo/filename" => 1, "foo/bar/filename => 2, "foo/bar/../filename => 1.

Author

Norman Walsh, <ndw@nwalsh.com>

Source Code

(define (directory-depth pathname)
  ;; Count the directory depth of a path name
  (let loop ((count 0) (pathlist (match-split pathname "/")))
    (if (null? pathlist)
	(- count 1) ;; pathname should always end in a filename
	(if (or (equal? (car pathlist) "/") (equal? (car pathlist) "."))
	    (loop count (cdr pathlist))
	    (if (equal? (car pathlist) "..")
		(loop (- count 1) (cdr pathlist))
		(loop (+ count 1) (cdr pathlist)))))))