Skip to content

escape characters in shell commands

3 messages · James Rome, Steven Kennedy, Gabor Grothendieck

#
On a Windows platform I am trying to count the number of lines in a file.
In a DOS window, the following works:
C:\Users\jar>findstr /R /N "^" D:\my_dir\my_file | find /C ":"
5317
(it works with double \\ also)

But in R, I need to make this string up with the file name I get from
file.choose():
filename = file.choose()
#get the number of lines in the file
# first make a command string with the filename in it
cmd = paste('findstr /R /N "^" ', filename, ' | find /C ":"', sep=""')
nrec = as.numeric(shell(cmd))

But R puts in escape characters for the ":
[1] "findstr /R /N \"^\" D:\\my_dir\\my_file | find /C \":\""

and shell(cmd) does not work properly. And
[1] 0
does not work either.

What am I doing wrong?

Thanks,
Jim
#
As a different approach, you could just use the readLines function:
length(readLines(D:\my_dir\my_file))
#
On Tue, May 24, 2011 at 1:40 PM, James Rome <jamesrome at gmail.com> wrote:
Just a guess but note that Rtools has a find.exe so if you have Rtools
on your path before C:\Windows\System32 then the above won't work. You
can check it via:

shell("where find.exe")

If that is the problem then workarounds include using the full path to
find, using wc (which is included in Rtools) or doing it entirely in
R.