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
.
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/
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 ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With