emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: how having the basename of a file or directory


From: Thierry Volpiatto
Subject: Re: how having the basename of a file or directory
Date: Mon, 31 Jan 2011 17:30:26 +0100
User-agent: Gnus/5.110011 (No Gnus v0.11) Emacs/23.2.92 (gnu/linux)

Stefan Monnier <address@hidden> writes:

>> Yes thanks, that's work too, but it would be nice to do not have to take
>> care of that:
>
>> (file-name-nondirectory "path/to/a/directory")
>> should return ==> directory
>
> It does.
>
>> (file-name-nondirectory "path/to/a/file")
>> should return ==> file
>
> It does.
>
>> So modifying `file-name-nondirectory' or creating a basename function
>> or macro like:
>
>> (defun basename (fname)
>>   (if (file-directory-p fname)
>>       (let ((dirname (directory-file-name fname)))
>>         (file-name-nondirectory dirname))
>>       (file-name-nondirectory fname)))
>
> That won't work on (basename "/non/existing/thing/").
> You really want to use (file-name-nondirectory (directory-file-name fname))
Indeed, yes, thanks Stefan, but something like


(defun basename (fname)
  (if (or (file-directory-p fname)
          (string-match "/$" fname))
      (let ((dirname (directory-file-name fname))) 
        (file-name-nondirectory dirname))
      (file-name-nondirectory fname)))

would work and be useful.

-- 
A+ Thierry
Get my Gnupg key:
gpg --keyserver pgp.mit.edu --recv-keys 59F29997 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]