[R] parsing dir output for file sizes

Griffith Feeney gfeeney at hawaii.edu
Tue Jul 11 18:54:58 CEST 2000

Thanks! I'd gotten as far as the string containing the file size but wasn't 
sure about this approach and, crucially if absurdly, couldn't get rid of 
the comma. regexpr() got me to sub(",", "", res), which does the trick.

BTW, I was using system("command.com /c ..."), which works on Win98.

At 09:03 PM 10/07/2000 -1000, you wrote:
>On Mon, 10 Jul 2000, Griffith Feeney wrote:
> > I've got an R wrapper around an old DOS program. In the R program I 
> need to
> > test whether the DOS program failed to produce certain output. This is
> > indicated by certain text files created by the DOS program being empty.
> >
> > I can use system(command, intern=TRUE) to get the output of a DOS dir 
> for a
> > test file, but I'm having trouble parsing this to get the file size.
> >
> > Is there an R function to find the first occurrence of a character in a
> > character string (seems there must be, but I'm at a loss to find it)? Or
> > can anyone suggest a better approach?
>regexpr() is one useful tool for this.  For example:
>txt <- "regexpr is the tool for this."
>regexpr("f", txt)
>[1] 21
>[1] 1
>I'm not at all sure what this has to do with the problem, though.
>Here is how I would do it.
>1) res <- shell("dir", intern=TRUE).  At least on my NT machine, "dir" is
>a DOS internal command, and is not found by system().
>2) The file sizes and file names are at fixed columns, so something like
>(not the right numbers)
>fs <- substring(res, 41, 49)
>fn <- substring(res, 51, 58)
>will give you the file sizes (as character strings) and file names.
>One day fairly soon we will add things like this as cross-platform
>inbuilt functions in R.
>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 272860 (secr)
>Oxford OX1 3TG, UK                Fax:  +44 1865 272595

r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list