Error : 'dir' command not found while using System function in R

Viewed 874

I want to use R's System function in my code and so I had written simple one-liner to check whether it's properly running or not. So this was my one liner -

system("dir", intern = TRUE)

I am using Windows so it should give me the same output as it would give on Command Prompt (show all the files present in current directory). But instead it resulted in an error:

Error in system("dir", intern = TRUE) : 'dir' not found

1 Answers

From the description of ?system:

command must be an executable (extensions ‘.exe’, ‘.com’) or a batch file (extensions ‘.cmd’ and ‘.bat’): these extensions are tried in turn if none is supplied. This means that redirection, pipes, DOS internal commands, ... cannot be used: see shell if you want to pass a shell command-line.

So, DOS internal commands cannot be used. It actually doesn't return an error for me, but it does not return anything.

However, shell works as expected:

 shell('dir', intern = TRUE)
 #[1] " Volume in drive C is "                                                  
 #[2] " Volume Serial Number is "                                            
 #[3] ""                                                                              
 #[4] " Directory of C:\\Users\\TB\\Documents"                             
 #[5] ""                                                                              
 #[6] "07/06/2019  11:10    <DIR>          ."     
 #.....                                    
Related