[Rd] Question about Unix file paths

Gabor Grothendieck ggrothendieck at myway.com
Thu Nov 27 15:39:16 MET 2003

> On Wed, 26 Nov 2003 16:09:34 +0100 (CET), you wrote:
> ># aldus John W. Eaton :
> >
> >> It seems to me that using this approach to implement a proper glob()
> >> function would be more work than using the glob code that is available
> >> as part of bash, which I think will allow you to handle much more
> >> complex patterns, including [xyz] {a,b,c} etc.
> >
> >Unix people don't need a glob function in R. But a simple glob,
> >with just '*' and '?', may be all that an average Windows user
> >can handle, and useful to them.
> We already have that, in choose.files(). It's interactive; maybe it
> should have a non-interactive option.
> I don't think we should add another pattern matching syntax to R.
> Filename pattern matching is a job for the shell or the OS.
> Duncan Murdoch

Its not done by the shell in Windows, VAX/VMS
and probably a number of other systems.  

Also, I found it surprising tough in any
short obvious way as the Windows dir commands 
will not handle this directly if you need full 
pathnames to be returned.  In Windows:

- the dir command will not return the complete
  pathname, only the filename (unless you use the
  /s flag but then it recursively descends the
  directory tree which is not what I want)

- the dir command will not accept /'s which means
  I have to do the conversion to backslashes
  myself.  (I prefer to specify /'s so that I
  don't have to use double backslashes which R
  requires since \ is also the string escape

I spent some time on this and think that I now
have a solution that works pretty well and is
short but involves a trick that was not immediately 
obvious.  This trick was to use the Windows attrib 
command as it instead of the dir command.  attrib
does return complete pathnames.  It even handles
forward slash specifiers.  

The first line in the body of the function
executes the attrib command, the second line
closes the pipe and the third line checks whether
anything was found and, if so, strips off the
stuff before the pathname.

# tested on Windows 2000
list.files.glob <- function( spec ) {
   z <- readLines( con <- pipe( paste( "cmd /c attrib", spec ) ) )
   close( con )
   if ( !pmatch("File not found - ", z[[1]], nomatch = 0) )  substring(z,12)

# a couple of examples:
list.files.glob( "c:/myfolder/my*.dat" )
list.files.glob( "c:\\myfolder\\my*.dat" )

More information about the R-devel mailing list