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 ("/").
(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)))))))