Unlink a directory with leading and trailing space

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Unlink a directory with leading and trailing space

Mike White-3
Using paste without defining a separator to generate a directory name for
dir.create, I have inadvertently created a directory with a leading and
trailing space. I cannot now delete this directory with unlink or from
Windows explorer.  Any help deleting this directory would be appreciated.

Thanks
Mike White

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Unlink a directory with leading and trailing space

Duncan Murdoch
On 1/3/2006 11:01 AM, Mike White wrote:
> Using paste without defining a separator to generate a directory name for
> dir.create, I have inadvertently created a directory with a leading and
> trailing space. I cannot now delete this directory with unlink or from
> Windows explorer.  Any help deleting this directory would be appreciated.

What name did you end up with?  I don't have any problem removing the
directory " test " just by right-clicking and choosing Delete.  You can
also open a command window, and put the name in quotes, e.g.

rmdir " test "

I do see the problem in unlink().

Duncan Murdoch

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Unlink a directory with leading and trailing space

Brian Ripley
On Wed, 4 Jan 2006, Duncan Murdoch wrote:

> On 1/3/2006 11:01 AM, Mike White wrote:
>> Using paste without defining a separator to generate a directory name for
>> dir.create, I have inadvertently created a directory with a leading and
>> trailing space. I cannot now delete this directory with unlink or from
>> Windows explorer.  Any help deleting this directory would be appreciated.
>
> What name did you end up with?  I don't have any problem removing the
> directory " test " just by right-clicking and choosing Delete.  You can
> also open a command window, and put the name in quotes, e.g.
>
> rmdir " test "

But AFAICS Windows always drops trailing spaces.

> dir.create(" test ")
> dir()[1]
[1] " test"

and similarly at the command prompt.

> I do see the problem in unlink().

Which in turn is a problem in MSVCRT's _rmdir.  The usual trick here is
to use short path names instead, and that works e.g.

  unlink("TEST~1", recursive=TRUE)

R-devel has a shortPathName() function, so you could do

  unlink(shortPathName(" test "), recursive = TRUE)

(but I've now fixed the source code to use the short name).

--
Brian D. Ripley,                  [hidden email]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Unlink a directory with leading and trailing space

Mike White-3
Thanks for your suggestions.
I am using Windows 2000 Professional with R 2.1.0 and have replicated the
problem with the code below which creates the directory " testdir "

# pathin is the parent path and ends with /
pathtest<-paste(pathin, "testdir", "/")  # I forgot to include sep=""
dir.create(pathtest) # creates directory " testdir "

The directory cannot be deleted with the short name as below
setwd(pathin)
unlink("testdi~1")
unlink(" testdi~1")

>From the console I have also tried
rmdir " testdir "
but without success.

Mike

----- Original Message -----
From: "Prof Brian Ripley" <[hidden email]>
To: "Duncan Murdoch" <[hidden email]>
Cc: "Mike White" <[hidden email]>; <[hidden email]>
Sent: Wednesday, January 04, 2006 1:49 PM
Subject: Re: [R] Unlink a directory with leading and trailing space


> On Wed, 4 Jan 2006, Duncan Murdoch wrote:
>
> > On 1/3/2006 11:01 AM, Mike White wrote:
> >> Using paste without defining a separator to generate a directory name
for
> >> dir.create, I have inadvertently created a directory with a leading and
> >> trailing space. I cannot now delete this directory with unlink or from
> >> Windows explorer.  Any help deleting this directory would be
appreciated.

> >
> > What name did you end up with?  I don't have any problem removing the
> > directory " test " just by right-clicking and choosing Delete.  You can
> > also open a command window, and put the name in quotes, e.g.
> >
> > rmdir " test "
>
> But AFAICS Windows always drops trailing spaces.
>
> > dir.create(" test ")
> > dir()[1]
> [1] " test"
>
> and similarly at the command prompt.
>
> > I do see the problem in unlink().
>
> Which in turn is a problem in MSVCRT's _rmdir.  The usual trick here is
> to use short path names instead, and that works e.g.
>
>   unlink("TEST~1", recursive=TRUE)
>
> R-devel has a shortPathName() function, so you could do
>
>   unlink(shortPathName(" test "), recursive = TRUE)
>
> (but I've now fixed the source code to use the short name).
>
> --
> Brian D. Ripley,                  [hidden email]
> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> University of Oxford,             Tel:  +44 1865 272861 (self)
> 1 South Parks Road,                     +44 1865 272866 (PA)
> Oxford OX1 3TG, UK                Fax:  +44 1865 272595
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Unlink a directory with leading and trailing space

Brian Ripley
On Wed, 4 Jan 2006, Mike White wrote:

> Thanks for your suggestions.
> I am using Windows 2000 Professional with R 2.1.0 and have replicated the

So as the posting guide says, upgrade if you have a problem: there are
three later versions of R.

> problem with the code below which creates the directory " testdir "
>
> # pathin is the parent path and ends with /
> pathtest<-paste(pathin, "testdir", "/")  # I forgot to include sep=""

using file.path() might help you out here ....

> dir.create(pathtest) # creates directory " testdir "

I get " testdir".

> The directory cannot be deleted with the short name as below
> setwd(pathin)
> unlink("testdi~1")
> unlink(" testdi~1")

Of course not!  Those do not correspond to the example given or to the
help page's

      If 'recursive = FALSE' directories are not deleted, not even empty
      ones.

which is why my example used recursive=TRUE.

>
> From the console I have also tried
> rmdir " testdir "
> but without success.
>
> Mike
>
> ----- Original Message -----
> From: "Prof Brian Ripley" <[hidden email]>
> To: "Duncan Murdoch" <[hidden email]>
> Cc: "Mike White" <[hidden email]>; <[hidden email]>
> Sent: Wednesday, January 04, 2006 1:49 PM
> Subject: Re: [R] Unlink a directory with leading and trailing space
>
>
>> On Wed, 4 Jan 2006, Duncan Murdoch wrote:
>>
>>> On 1/3/2006 11:01 AM, Mike White wrote:
>>>> Using paste without defining a separator to generate a directory name
> for
>>>> dir.create, I have inadvertently created a directory with a leading and
>>>> trailing space. I cannot now delete this directory with unlink or from
>>>> Windows explorer.  Any help deleting this directory would be
> appreciated.
>>>
>>> What name did you end up with?  I don't have any problem removing the
>>> directory " test " just by right-clicking and choosing Delete.  You can
>>> also open a command window, and put the name in quotes, e.g.
>>>
>>> rmdir " test "
>>
>> But AFAICS Windows always drops trailing spaces.
>>
>>> dir.create(" test ")
>>> dir()[1]
>> [1] " test"
>>
>> and similarly at the command prompt.
>>
>>> I do see the problem in unlink().
>>
>> Which in turn is a problem in MSVCRT's _rmdir.  The usual trick here is
>> to use short path names instead, and that works e.g.
>>
>>   unlink("TEST~1", recursive=TRUE)
>>
>> R-devel has a shortPathName() function, so you could do
>>
>>   unlink(shortPathName(" test "), recursive = TRUE)
>>
>> (but I've now fixed the source code to use the short name).
>>
>> --
>> Brian D. Ripley,                  [hidden email]
>> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
>> University of Oxford,             Tel:  +44 1865 272861 (self)
>> 1 South Parks Road,                     +44 1865 272866 (PA)
>> Oxford OX1 3TG, UK                Fax:  +44 1865 272595
>>
>
>
>

--
Brian D. Ripley,                  [hidden email]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Unlink a directory with leading and trailing space

Duncan Murdoch
In reply to this post by Mike White-3
On 1/4/2006 10:23 AM, Mike White wrote:

> Thanks for your suggestions.
> I am using Windows 2000 Professional with R 2.1.0 and have replicated the
> problem with the code below which creates the directory " testdir "
>
> # pathin is the parent path and ends with /
> pathtest<-paste(pathin, "testdir", "/")  # I forgot to include sep=""
> dir.create(pathtest) # creates directory " testdir "
>
> The directory cannot be deleted with the short name as below
> setwd(pathin)
> unlink("testdi~1")
> unlink(" testdi~1")
>
>>From the console I have also tried
> rmdir " testdir "
> but without success.

I just tried this, and confirmed your trouble with unlink (but haven't
incorporated Brian's change, as below).

However, from the console "rmdir TESTDI~1" worked for me.

I've just tried again, and this time list.files() shows the name to have
been created as " testdir", not " testdir " (as Brian mentioned); in the
console

rmdir " testdir"

worked.

Duncan Murdoch

>
> Mike
>
> ----- Original Message -----
> From: "Prof Brian Ripley" <[hidden email]>
> To: "Duncan Murdoch" <[hidden email]>
> Cc: "Mike White" <[hidden email]>; <[hidden email]>
> Sent: Wednesday, January 04, 2006 1:49 PM
> Subject: Re: [R] Unlink a directory with leading and trailing space
>
>
>> On Wed, 4 Jan 2006, Duncan Murdoch wrote:
>>
>> > On 1/3/2006 11:01 AM, Mike White wrote:
>> >> Using paste without defining a separator to generate a directory name
> for
>> >> dir.create, I have inadvertently created a directory with a leading and
>> >> trailing space. I cannot now delete this directory with unlink or from
>> >> Windows explorer.  Any help deleting this directory would be
> appreciated.
>> >
>> > What name did you end up with?  I don't have any problem removing the
>> > directory " test " just by right-clicking and choosing Delete.  You can
>> > also open a command window, and put the name in quotes, e.g.
>> >
>> > rmdir " test "
>>
>> But AFAICS Windows always drops trailing spaces.
>>
>> > dir.create(" test ")
>> > dir()[1]
>> [1] " test"
>>
>> and similarly at the command prompt.
>>
>> > I do see the problem in unlink().
>>
>> Which in turn is a problem in MSVCRT's _rmdir.  The usual trick here is
>> to use short path names instead, and that works e.g.
>>
>>   unlink("TEST~1", recursive=TRUE)
>>
>> R-devel has a shortPathName() function, so you could do
>>
>>   unlink(shortPathName(" test "), recursive = TRUE)
>>
>> (but I've now fixed the source code to use the short name).
>>
>> --
>> Brian D. Ripley,                  [hidden email]
>> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
>> University of Oxford,             Tel:  +44 1865 272861 (self)
>> 1 South Parks Road,                     +44 1865 272866 (PA)
>> Oxford OX1 3TG, UK                Fax:  +44 1865 272595
>>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html