6

I have the following string I'm trying to pass to system on a Win 7 machine. It should be creating the .git directory in a repo but using system it does not (though a similar approach does work on a Linux box so this is a Windows specific problem).

system( "cd C:/Users/trinker/Desktop/foo2 && \"C:\\Program Files (x86)\\Git\\bin\\git.exe\" init" )

C:/Users/trinker/Desktop/foo2 is the location of a repo. C:\\Program Files (x86)\\Git\\bin\\git.exe is the location of git on my system.

When I run the above nothing happens. No message, nadda. But is I run cat on the string and paste it directly into the command line it runs, gives the following message and creates .git in the appropriate place.

So running...

cat("cd C:/Users/trinker/Desktop/foo2 && \"C:\\Program Files (x86)\\Git\\bin\\git.exe\" init")

Pasting this into the command line...

cd C:/Users/trinker/Desktop/foo2 && "C:\Program Files (x86)\Git\bin\git.exe" init

Gives...

Initialized empty Git repository in C:/Users/trinker/Desktop/foo2/.git/

...Which is good

So I can do it outside of R with the same string but not within R. What do I need to do to the first string where I use system to make it run as if though I cat and pasted into the command line? An answer is great but I'd like to know what's going on here so I can address similar circumstances in the future with accessing the Windows command line with system.

Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519

2 Answers2

5

On windows use shell. This works just fine for me...

shell( "cd C:/Data/foo2 && \"C:\\Program Files (x86)\\Git\\bin\\git.exe\" init" )
#CMD.EXE was started with the above path as the current directory.
#UNC paths are not supported.  Defaulting to Windows directory.
#Initialized empty Git repository in C:/Data/foo2/.git/
Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
  • Seems like `shell` is a much nicer approach on windows. I wonder if git.exe would be on the path via shell (of course this would depend on the system though) – Dason Sep 06 '13 at 04:59
  • @Simon, do you think you could help with this question ? http://stackoverflow.com/questions/32679330/how-to-get-the-cpu-temperature-info-using-r – rafa.pereira Sep 23 '15 at 09:53
3

Give this a try - at least for me using system("cd blah blah && blah blah", intern = TRUE) gave Error in system(cmd, intern = T) : 'cd' not found so using cd is out - luckily the working directory is used so you can just change the working directory in R instead of in a system call.

wd <- getwd()
setwd("C:/Users/trinker/Desktop/foo2")
cmd <- '"C:/Program Files (x86)/Git/bin/git.exe" init'
system(cmd, intern = T)
setwd(wd)

The intern parameter isn't necessary but it can help for debugging.

I'm just thankful I typically run on Linux ;)

Dason
  • 60,663
  • 9
  • 131
  • 148