“rm -rf” equivalent for Windows?

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

    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

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

    Using Powershell 5.1

     get-childitem *logs* -path .\ -directory -recurse | remove-item -confirm:$false -recurse -force
    

    Replace logs with the directory name you want to delete.

    get-childitem searches for the children directory with the name recursively from current path (.).

    remove-item deletes the result.

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

    del /s /q directorytobedeleted

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

    For deleting a directory (whether or not it exists) use the following:

    if exist myfolder ( rmdir /s/q myfolder )
    
    0 讨论(0)
  • 2020-11-27 09:30

    The accepted answer is great, but assuming you have Node installed, you can do this much more precisely with the node library "rimraf", which allows globbing patterns. If you use this a lot (I do), just install it globally.

    yarn global add rimraf
    

    then, for instance, a pattern I use constantly:

    rimraf .\**\node_modules
    

    or for a one-liner that let's you dodge the global install, but which takes slightly longer for the the package dynamic download:

    npx rimraf .\**\node_modules
    
    0 讨论(0)
  • 2020-11-27 09:30

    here is what worked for me:

    Just try decreasing the length of the path. i.e :: Rename all folders that lead to such a file to smallest possible names. Say one letter names. Go on renaming upwards in the folder hierarchy. By this u effectively reduce the path length. Now finally try deleting the file straight away.

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