Skip to content

Querying the path separator?

8 messages · Henrik Bengtsson, Jeff Newmiller, Jonathan Greenberg +2 more

#
Hopefully this is a pretty easy fix -- I need to have R query the path
separator for some code I'm trying to write (it involves using a
system() call) -- the call requires a path and a wildcard, e.g.:

command="mycommand /path/to/*.files" in the case of unix or,
command="mycommand.exe C:\\path\\to\\*.files" on a windows box

System.which is working correctly, so the "mycommand" vs
"mycommand.exe" part is working fine.  The issue is that the /path/to
should be set to getwd(), but this strips the trailing path separator.
 How do I go about querying the correct path separator for the system,
so I can include it in a paste command (via sep=)?  Thanks!

--j
#
See file.path().

/Henrik

On Tue, Jun 28, 2011 at 6:59 PM, Jonathan Greenberg
<greenberg at ucdavis.edu> wrote:
#
Thanks Henrik, but as the help for this function states:

Note
The components are separated by / (not \) on Windows.

, the slashes it uses are incorrect for use in a CMD window on a
Windows box (they need to be \ but file.path uses /).  I suppose I can
do a query as to whether the platform is Windows (in which case use \)
or other (in which case use /) -- there isn't a more "clever" way of
doing this?

--j
On Tue, Jun 28, 2011 at 7:10 PM, Henrik Bengtsson <hb at biostat.ucsf.edu> wrote:

  
    
#
The problem is that I'm trying to create a path to use with a system()
call, and the command window will not work with the forward slash,
only with the standard backslash.  I do understand that within R it
will work with either way, but not via the system call.  I'm trying to
create a "generic" system() call that will work with an external
executable that is available on both windows and unix machines.

--j



On Tue, Jun 28, 2011 at 10:02 PM, Jeff Newmiller
<jdnewmil at dcn.davis.ca.us> wrote:

  
    
#
Jonathan Greenberg wrote:
Would .Platform$file.sep help you?
I can't test this.

Berend

--
View this message in context: http://r.789695.n4.nabble.com/Querying-the-path-separator-tp3631806p3632161.html
Sent from the R help mailing list archive at Nabble.com.
1 day later
#
Unfortunately, no.  .Platform$file.sep on a windows box is the "/".
For this DOS program it needs to be the "classic" backslash.

Ok, we'll hack this a bit (query the platform and test if it is
windows), but now I'm running into escape character problems.  Given:

files1="*.las"
files1_formatted=file.path(getwd(),files1)
files1_formatted
[1] "X:/pathto/*.las"

I want files1_formatted to read:
"X:\pathto\*.las"

Using:
sub("/","\",files1_formatted)
Doesn't work (in fact, its not a complete statement -- I assume since
its waiting for something to follow the / ).  What's the trick with
this?

--j
On Tue, Jun 28, 2011 at 11:05 PM, Berend Hasselman <bhh at xs4all.nl> wrote:

  
    
#
On 30/06/2011 3:32 PM, Jonathan Greenberg wrote:
The problem is with the backslash, which is an escape character in R.  
Use this instead:

gsub("/", "\\", files1_formatted, fixed=TRUE)

The "g" in gsub says do them all, the fixed=TRUE says to interpret 
everything as plain characters, not regular expressions (which would 
need extra escaping).  It will display with escaped backslashes, but 
there's really just one in there, as cat() will show.

Duncan Murdoch