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.

9
  • What happens if you pass your string through shQuote first? Commented Sep 4, 2013 at 1:36
  • I think you're on to the correct response but: system(shQuote("cd C:/Users/trinker/Desktop/foo2 && \"C:\\Program Files (x86)\\Git\\bin\\git.exe\" init")) gives the same result of nothing. Commented Sep 4, 2013 at 1:40
  • Maybe shQuote is struggling to handle the escaped quotation marks? Commented Sep 4, 2013 at 1:44
  • I tried removing that as well. I also tried running shQuote on just the git location and pasting together into system with the same result. Commented Sep 4, 2013 at 1:49
  • Try system( 'cd C:/Users/trinker/Desktop/foo2 && \"C:\\Program Files (x86)\\Git\\bin\\git.exe\" init' ) Commented Sep 4, 2013 at 1:50

2 Answers 2

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/
Sign up to request clarification or add additional context in comments.

2 Comments

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)
@Simon, do you think you could help with this question ? stackoverflow.com/questions/32679330/…
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 ;)

1 Comment

Boom Perfect that does it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.