“rm -rf” equivalent for Windows?

前端 未结 21 1362
一向
一向 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:18

    admin:

    takeown /r /f folder
    cacls folder /c /G "ADMINNAME":F /T
    rmdir /s folder
    

    Works for anything including sys files

    EDIT: I actually found the best way which also solves file path too long problem as well:

    mkdir \empty
    robocopy /mir \empty folder
    
    0 讨论(0)
  • 2020-11-27 09:22

    You can install GnuWin32 and use *nix commands natively on windows. I install this before I install anything else on a minty fresh copy of windows. :)

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

    USE AT YOUR OWN RISK. INFORMATION PROVIDED 'AS IS'. NOT TESTED EXTENSIVELY.

    Right-click Windows icon (usually bottom left) > click "Windows PowerShell (Admin)" > use this command (with due care, you can easily delete all your files if you're not careful):

    rd -r -include *.* -force somedir
    

    Where somedir is the non-empty directory you want to remove.

    Note that with external attached disks, or disks with issues, Windows sometimes behaves odd - it does not error in the delete (or any copy attempt), yet the directory is not deleted (or not copied) as instructed. (I found that in this case, at least for me, the command given by @n_y in his answer will produce errors like 'get-childitem : The file or directory is corrupted and unreadable.' as a result in PowerShell)

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

    RMDIR or RD if you are using the classic Command Prompt (cmd.exe):

    rd /s /q "path"
    

    RMDIR [/S] [/Q] [drive:]path

    RD [/S] [/Q] [drive:]path

    /S Removes all directories and files in the specified directory in addition to the directory itself. Used to remove a directory tree.

    /Q Quiet mode, do not ask if ok to remove a directory tree with /S

    If you are using PowerShell you can use Remove-Item (which is aliased to del, erase, rd, ri, rm and rmdir) and takes a -Recurse argument that can be shorted to -r

    rd -r "path"
    
    0 讨论(0)
  • 2020-11-27 09:23

    Go to the path and trigger this command.

    rd /s /q "FOLDER_NAME"
    

    /s : Removes the specified directory and all subdirectories including any files. Use /s to remove a tree.

    /q : Runs rmdir in quiet mode. Deletes directories without confirmation.

    /? : Displays help at the command prompt.

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

    There is also deltree if you're on an older version of windows.

    I really like this site for finding commands: SS64: Del - Delete Files

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