[R] Unlink a directory with leading and trailing space

Duncan Murdoch murdoch at stats.uwo.ca
Wed Jan 4 16:56:24 CET 2006


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" <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
>>
>




More information about the R-help mailing list