RM -rf equivalent for Windows?

匿名 (未验证) 提交于 2019-12-03 09:05:37

问题:

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 delete directories.

DELTREE was removed from Windows 2000+

回答1:

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

rd /s /q "path"

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"


回答2:

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


回答3:

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



回答4:

You can install cygwin, which has rm as well as ls etc.



回答5:

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.



回答6:

rmdir /S /Q %DIRNAME%



回答7:

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

if exist myfolder ( rmdir /s/q myfolder )


回答8:

rmdir /s dirname



回答9:

Try this command:

del /s foldername


回答10:

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. :)



回答11:

Here is what you need to do...

Create a batch file with the following line

RMDIR /S %1

Save your batch file as Remove.bat and put it in C:\windows

Create the following registry key

HKEY_CLASSES_ROOT\Directory\shell\Remove Directory (RMDIR)

Launch regedit and update the default value HKEY_CLASSES_ROOT\Directory\shell\Remove Directory (RMDIR)\default with the following value

"c:\windows\REMOVE.bat" "%1"

Thats it! Now you can right click any directory and use the RMDIR function



回答12:

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.



回答13:

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



回答14:

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.



回答15:

del /s /q directorytobedeleted



回答16:

http://www.computerhope.com/delhlp.htm

del /s


转载请标明出处:RM -rf equivalent for Windows?
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!