[R] parsing dir output for file sizes

Prof Brian D Ripley ripley at stats.ox.ac.uk
Tue Jul 11 09:00:27 CEST 2000

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