[R] Unlink a directory with leading and trailing space

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

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