process and spaces in filenames

Q&A's, tips, howto's
Locked
cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

process and spaces in filenames

Post by cormullion »

Just before my brain packs up for the day:

(set 'f "/Users/me/folder 1/file 1") ; yes, spaces in pathnames...
(process (append "ls -l " f )) ; doesn't work

I need to pass some kind of quoted form of f to the shell. What's the best way of doing that?

Sorry if the question is a bit too simple...!

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

Hi Cormullion,

Does this thread perhpas help? ->
http://www.alh.net/newlisp/phpbb/viewtopic.php?t=843

Regards, Norman.
-- (define? (Cornflakes))

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

Works fine for me:

Code: Select all

> (set 'f "/Users")
"/Users" 
> (process (append "ls -l " f))
1270 
> total 0
drwxrwxrwt    4 root  wheel   136 Oct 20 03:36 Shared
drwxr-xr-x   45 lutz  lutz   1530 Dec  1 10:37 lutz
But it works better with 'exec', because you get the output returned in a list:

Code: Select all

> (exec (append "ls -l " f))
("total 0" "drwxrwxrwt    4 root  wheel   136 Oct 20 03:36 Shared" 
 "drwxr-xr-x   45 lutz  lutz   1530 Dec  1 10:37 lutz") 
> 
and you also could do this:

Code: Select all

> (directory "/Users/")
("." ".." ".DS_Store" ".localized" "lutz" "Shared") 
> 
Lutz

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

Hi folks. Thanks for the replies. But the problem is with files and directories that have spaces in their names. (Perhaps this is a MacOS X-only thing?) So this works:

"/Applications/Firefox.app/"

but this doesn't:

"/Applications/Address Book.app/"

Loads of files and directories have spaces in, and I was looking for a way to quote them for the (process ) or (exec) functions.

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

Unix space you have to pre with an "\" like in

{/this\ is\ a\ directory}

Enjoy...
-- (define? (Cornflakes))

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

Thanks. I've also just had this inspiration:

(set 'f "/Applications/Address Book.app/")
(exec (string "ls -l " (format "'%s'" f )))

- a way of putting single quotes round the string. If there's nothing better, that'll do nicely!

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

... and you could shorten that to:

Code: Select all

(exec (format "ls -l '%s'" f ))
because format returns a string and can take other text besides format specs

Lutz

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

Neat. Thanks.

Locked