[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Fail stating a file not in current folder
From: |
Neil Jerram |
Subject: |
Re: Fail stating a file not in current folder |
Date: |
Wed, 17 Sep 2008 09:59:24 +0100 |
2008/9/17 zulian jc <address@hidden>:
> Hello guile list,
Hi!
> I am new to guile and scheme so excuse my poor coding. Anyway I am faced with
> a little problem with guile 1.8.1. I am writing a function that collects all
> sub-directory from a given directory. To do this I am using the opendir and
> readdir functions. However it happens that if I am not cd'ing into the
> directory I want to browse, the following function failed. Is that a normal
> behavior?
Yes, because of what you have suggested...
> (define gather-dirs
> (lambda (path)
> ;; with the following line commented out the function will fail browsing
> ;; directories other than the current one
> ;;(chdir path)
> (let ((cdir (opendir path))
> (l '()))
> (do ((entry (readdir cdir) (readdir cdir)))
> ((eof-object? entry) l)
> (if (directory? entry)
> (set! l (cons entry l)))))))
>
> (define directory?
> (lambda (x)
> (eq? (stat:type (stat x)) 'directory)))
>
> Here is the error I get back:
> ERROR: In procedure stat:
> ERROR: No such file or directory: ".bb"
>
> Note: '.bb' is a folder that exist in the folder I want to browse but not in
> the current one. So basicaly 'readdir' and 'opendir' did their job fine but
> when I am trying to stat on the found entry it fails (in the 'directory?'
> function).
>
> Investigating further I saw that 'readdir' just return a string with the name
> of the folder entry, that is a name relative to the folder opened
> by 'opendir'. Say I am browsing '/home/bob' folder then readdir will return
> me as an entry "bin" and not "/home/bob/bin". Hence 'stat' is failing if
> there is no 'bin' folder in current folder. Is that correct?
>
> Now how could one browse a folder and stat on each entry without having to
> change to that folder?
You can use `in-vicinity' to form a relative path name from the
working directory. Your code above should work if you change the
(directory? entry) call to:
(directory? (in-vicinity path entry))
Regards,
Neil