[R] Unlink a directory with leading and trailing space

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Jan 4 16:49:38 CET 2006


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" <ripley at stats.ox.ac.uk>
> To: "Duncan Murdoch" <murdoch at stats.uwo.ca>
> Cc: "Mike White" <mikewhite.diu at btconnect.com>; <R-help at stat.math.ethz.ch>
> 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,                  ripley at stats.ox.ac.uk
>> 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,                  ripley at stats.ox.ac.uk
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




More information about the R-help mailing list