For Programmers: Free Programming Magazines  


Home > Archive > Matlab > August 2007 > Remove a folder with space character









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Remove a folder with space character
Mifano

2007-08-29, 8:15 am

I am using Matlab R2006b.
When I use the function rmdir_r13(pathname):

function rmdir_r13(dirName)
if exist(dirName, 'dir')
if ispc
dos(['rmdir /S/Q ' dirName]);
else
unix(['rm -f -R ' dirName]);
end
end

to remove a folder. However, this function works only if the
folder's path does not content space character (' '). I
thought that this error is already fixed in R2006b?

Could you tell me how can I fixe this error?
Thank you very much
Michael Wild

2007-08-29, 8:15 am

Mifano wrote:
> I am using Matlab R2006b.
> When I use the function rmdir_r13(pathname):
>
> function rmdir_r13(dirName)
> if exist(dirName, 'dir')
> if ispc
> dos(['rmdir /S/Q ' dirName]);
> else
> unix(['rm -f -R ' dirName]);
> end
> end
>
> to remove a folder. However, this function works only if the
> folder's path does not content space character (' '). I
> thought that this error is already fixed in R2006b?
>
> Could you tell me how can I fixe this error?
> Thank you very much


problem is, you'll have to quote it for the shell. else the shell
interpreter (matlab has nothing to do with that) thinks it is two
separate arguments to rmdir/rm.

unix(['rm -f -R ' '"' dirname '"']);


hth

michael
Mifano

2007-08-30, 4:29 am

Michael Wild <themiwi.REMOVE.THIS@student.ethz.ch> wrote in
message <46d55b4f$1@news1-rz-ap.ethz.ch>...
> Mifano wrote:
>
> problem is, you'll have to quote it for the shell. else

the shell
> interpreter (matlab has nothing to do with that) thinks it

is two
> separate arguments to rmdir/rm.
>
> unix(['rm -f -R ' '"' dirname '"']);
>
>
> hth
>
> michael



Thank you for your reply Michael but I'm not sure that I
understand what you mean when you say "quote it for the
shell". Could you tell me more in details please?
Thank you.
Michael Wild

2007-08-30, 4:29 am

Mifano wrote:
> Michael Wild <themiwi.REMOVE.THIS@student.ethz.ch> wrote in
> message <46d55b4f$1@news1-rz-ap.ethz.ch>...
> the shell
> is two
>
>
> Thank you for your reply Michael but I'm not sure that I
> understand what you mean when you say "quote it for the
> shell". Could you tell me more in details please?
> Thank you.



when you do

dirName = '/path to/some directory/with spaces';
unix(['rm -f -R ' dirName]);

you generate the following command string:

cmd = 'rm -f -R /path to/some directory/with spaces';

what matlab does, is just passing this string cmd to the operating
system shell. so, what the shell sees, is the command

rm -f -R /path to/some directory/with spaces

and because of the spaces it thinks you are passing multiple arguments
instead of a single one containing spaces, resulting in the equivalent of

rm -f -R /path
rm -f -R to/some
rm -f -R directory/with
rm -f -R spaces

which definitely is NOT what you want.

So, either you have to escape the spaces using \ , or you have to quote
it as a string:

rm -f -R /path\ to/some\ directory/with\ spaces
rm -f -R "/path to/some directory/with spaces"

Where the first option only works on posix (i.e. unix/linux/mac) shells.


To achieve the second option, use the command I showed you:

unix(['rm -f -R ' '"' dirname '"']);

or use sprintf:

unix(sprintf('rm -f -R "%s"',dirname));


I hope I made things clear and you understand why it isn't Matlab's fault...


Michael
Mifano

2007-08-30, 4:29 am

Michael Wild <themiwi.REMOVE.THIS@student.ethz.ch> wrote in
message <46d68377$1@news1-rz-ap.ethz.ch>...
> Mifano wrote:
if the[color=darkred]
>
>
> when you do
>
> dirName = '/path to/some directory/with spaces';
> unix(['rm -f -R ' dirName]);
>
> you generate the following command string:
>
> cmd = 'rm -f -R /path to/some directory/with spaces';
>
> what matlab does, is just passing this string cmd to the

operating
> system shell. so, what the shell sees, is the command
>
> rm -f -R /path to/some directory/with spaces
>
> and because of the spaces it thinks you are passing

multiple arguments
> instead of a single one containing spaces, resulting in

the equivalent of
>
> rm -f -R /path
> rm -f -R to/some
> rm -f -R directory/with
> rm -f -R spaces
>
> which definitely is NOT what you want.
>
> So, either you have to escape the spaces using \ , or you

have to quote
> it as a string:
>
> rm -f -R /path\ to/some\ directory/with\ spaces
> rm -f -R "/path to/some directory/with spaces"
>
> Where the first option only works on posix (i.e.

unix/linux/mac) shells.
>
>
> To achieve the second option, use the command I showed you:
>
> unix(['rm -f -R ' '"' dirname '"']);
>
> or use sprintf:
>
> unix(sprintf('rm -f -R "%s"',dirname));
>
>
> I hope I made things clear and you understand why it isn't

Matlab's fault...
>
>
> Michael



OK, thank you for your very clear explanation.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com