Page 1 of 1
Find parent directory of file
Posted: Tue Jul 04, 2006 5:42 pm
by cormullion
Given a file coming in through the command line:
How can I find out what the directory of it is so that I can set the current directory to it?
The only thing I can see to do is to parse the filename, which seems like the non-newLISP way...
Posted: Tue Jul 04, 2006 6:05 pm
by Lutz
you could use
real-path to get the full path and then isolate the path chopping off the filename from the end:
Code: Select all
(join (chop (parse (real-path (main-args 2)) "/")) "/")
Lutz
Posted: Tue Jul 04, 2006 9:13 pm
by cormullion
Thanks, Lutz! I was trying things like parse and even stuff like:
Code: Select all
(reverse (member "/" (reverse (real-path f) )))
Yours looks more sensible.
I can imagine a more elegant solution one day - like if change-dir accepted a filename and found the containing directory ...
Posted: Tue Jul 04, 2006 9:31 pm
by Sammo
Following Lutz's lead, in Windows I can define:
Code: Select all
(define (.. f)
(join (chop (parse (real-path f) "\\") 2) "\\") )
(define (. f)
(join (chop (parse (real-path f) "\\") 1) "\\") )
Then execute:
Code: Select all
(change-dir (.. "c:\\path1\\path2\\path3\\filename.txt"))
to change the directory focus to c:/path1/path2, or:
Code: Select all
(change-dir (. "c:\\path1\\path2\\path3\\filename.txt"))
to change the directory focus to c:/path1/path2/path3.
Something along these lines might be the ticket.
Posted: Tue Jul 04, 2006 9:59 pm
by cormullion
Hi Sammo - nice to see you round here! I hadn't thought about the Windows backslash. Doesn't Windows now accept forward slashes? (Sorry I rarely use it at the moment.) So a generic function would be harder.
I like the idea of defining '..' and '.' as functions...