Using a system with windows

I have the following line that I am trying to transfer to system on a Win 7 machine. It should create a .git directory in the repo, but it does not work with system (although this approach works in a Linux box, so this is a problem with Windows).

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

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

When I run above, nothing happens. No posts, over. But I run cat on the line and paste it directly into the command line it launches, it displays the following message and creates .git in the appropriate place.

This is how it works ...

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

Paste 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/ 

... What well

So I can do this outside of R with the same line, but not inside R. What do I need to do for the first line, where I use system so that it runs as if I were cat and inserted into the command line? The answer is great, but I would like to know what is happening here, so I can turn to similar circumstances in the future with access to the Windows command line using system .

+4
source share
2 answers

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 cd is not used - fortunately, the working directory is used, so you can simply change the working directory to R instead of 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 is optional, but it can help with debugging.

I am just thankful that I usually work on Linux;)

+3
source

In windows use shell . This works great 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/ 
+3
source

Source: https://habr.com/ru/post/1500414/


All Articles