Search code examples
rwindows

Windows rmdir fails when run in R system() function


When I run the following command at the Windows 11 Command Prompt it works:

rmdir /s /q C:\_studies\assess_strategy

However, when I run it from within the R 'system' function I get the following feedback:

system("rmdir /s /q C:\\_studies\\assess_strategy")
/usr/bin/rmdir: failed to remove '/s': No such file or directory
/usr/bin/rmdir: failed to remove '/q': No such file or directory
/usr/bin/rmdir: failed to remove 'C:\_studies\assess_strategy': Directory not empty

I've tried escaping /s and /q but that doesn't help:

system("rmdir \\/s \\/q C:\\_studies\\assess_strategy")

I get the same feedback as previously.

What can I do to get this to work?

By the way, I've also tried the R unlink command. That also fails.


Solution

  • From /usr/bin/rmdir in recieved error I assume it's not the rmdir you were expecting. It's probably from Git Bash, does not understand /s /q keys and removes only empty directories. rmdir / rd is an internal command, so try to invoke it through shell() instead:

    shell("rmdir /s /q C:\\_studies\\assess_strategy")
    

    For unlink(), make sure you have recursive enabled:

    unlink("C:\\_studies\\assess_strategy"", recursive = TRUE)
    

    Or just use fs::dir_delete()