“rm -rf” equivalent for Windows?

前端 未结 21 1355
一向
一向 2020-11-27 08:40

I need a way to recursively delete a folder and its children.

Is there a prebuilt tool for this, or do I need to write one?

DEL /S doesn\'t dele

相关标签:
21条回答
  • 2020-11-27 09:39

    rmdir /S /Q %DIRNAME%

    0 讨论(0)
  • 2020-11-27 09:43

    First, let’s review what rm -rf does:

    C:\Users\ohnob\things>touch stuff.txt
    
    C:\Users\ohnob\things>rm -rf stuff.txt
    
    C:\Users\ohnob\things>mkdir stuff.txt
    
    C:\Users\ohnob\things>rm -rf stuff.txt
    
    C:\Users\ohnob\things>ls -l
    total 0
    
    C:\Users\ohnob\things>rm -rf stuff.txt
    

    There are three scenarios where rm -rf is commonly used where it is expected to return 0:

    1. The specified path does not exist.
    2. The specified path exists and is a directory.
    3. The specified path exists and is a file.

    I’m going to ignore the whole permissions thing, but nobody uses permissions or tries to deny themselves write access on things in Windows anyways (OK, that’s meant to be a joke…).

    First set ERRORLEVEL to 0 and then delete the path only if it exists, using different commands depending on whether or not it is a directory. IF EXIST does not set ERRORLEVEL to 0 if the path does not exist, so setting the ERRORLEVEL to 0 first is necessary to properly detect success in a way that mimics normal rm -rf usage. Guarding the RD with IF EXIST is necessary because RD, unlike rm -f, will throw an error if the target does not exist.

    The following script snippet assumes that DELPATH is prequoted. (This is safe when you do something like SET DELPATH=%1. Try putting ECHO %1 in a .cmd and passing it an argument with spaces in it and see what happens for yourself). After the snippet completes, you can check for failure with IF ERRORLEVEL 1.

    : # Determine whether we need to invoke DEL or RD or do nothing.
    SET DELPATH_DELMETHOD=RD
    PUSHD %DELPATH% 2>NUL
    IF ERRORLEVEL 1 (SET DELPATH_DELMETHOD=DEL) ELSE (POPD)
    IF NOT EXIST %DELPATH% SET DELPATH_DELMETHOD=NOOP
    : # Reset ERRORLEVEL so that the last command which
    : # otherwise set it does not cause us to falsely detect
    : # failure.
    CMD /C EXIT 0
    IF %DELPATH_DELMETHOD%==DEL DEL /Q %DELPATH%
    IF %DELPATH_DELMETHOD%==RD RD /S /Q %DELPATH%
    

    Point is, everything is simpler when the environment just conforms to POSIX. Or if you install a minimal MSYS and just use that.

    0 讨论(0)
  • 2020-11-27 09:44

    LATE BUT IMPORTANT ANSWER to anyone who is having troubles installing npm packages on windows machine and if you are seeing error saying "rm -rf..." command not found. You can use the bash cli to run rm command on windows.

    for npm users, you can change the npm's config to npm config set script-shell "C:\Program Files\Git\bin\bash.exe" this way if the npm package you are trying to install has a post install script that uses rm -rf command, you will be able to run that rm command without needing to change anything in the npm package or disabling the post install scripts config. (For example, styled-components uses rm command in their post install scripts)

    If you want to just use the rm command, you can easily use the bash and pass the arguments.

    So yes, you can use the 'rm' command on windows.

    0 讨论(0)
提交回复
热议问题