none
How to delete directories with space in the name recursively? RRS feed

  • Question

  • I would like delete all sub-directories recursively, some of them have space in the directory name. Any suggestion on solving it via windows batch script? Thanks!
    • Edited by mail8mz Thursday, February 20, 2014 3:46 AM
    Thursday, February 20, 2014 3:35 AM

Answers

  • This command does NOT work for me, it deletes the c:\test folder as well.

    I am using the following command, it works fine for me.

    for /d %p in ("c:\test\*.*") do rmdir "%p" /s /q

    Any simpler solution is welcome.  Thanks!




    • Edited by mail8mz Thursday, February 20, 2014 5:00 PM
    • Marked as answer by mail8mz Thursday, February 20, 2014 5:56 PM
    Thursday, February 20, 2014 4:25 PM

All replies

  • This is a basic how to use Windows.   It is usually given before chapter 3 of and training book.

    rd /?

    rd c:\foldername  /s

    It is as old as Windows.

    Take some time out and get a book on basic Windows.  It will save you a lot of time.


    ¯\_(ツ)_/¯

    • Proposed as answer by jrv Thursday, February 20, 2014 4:05 AM
    • Unproposed as answer by Bill_StewartModerator Thursday, February 20, 2014 5:23 PM
    Thursday, February 20, 2014 4:04 AM
  • 'rd c:\foldername /s' won't work for me. I have to leave the parent folder there, only remove the sub folders.

    Thanks!

    • Edited by mail8mz Thursday, February 20, 2014 4:33 AM
    Thursday, February 20, 2014 4:19 AM
  • rd c:\test\ /s

    deletes all file and folders in the target.


    ¯\_(ツ)_/¯

    Thursday, February 20, 2014 4:28 AM
  • This command does NOT work for me, it deletes the c:\test folder as well.

    I am using the following command, it works fine for me.

    for /d %p in ("c:\test\*.*") do rmdir "%p" /s /q

    Any simpler solution is welcome.  Thanks!




    • Edited by mail8mz Thursday, February 20, 2014 5:00 PM
    • Marked as answer by mail8mz Thursday, February 20, 2014 5:56 PM
    Thursday, February 20, 2014 4:25 PM
    • Proposed as answer by jrv Thursday, February 20, 2014 4:53 PM
    Thursday, February 20, 2014 4:46 PM
  • This command does NOT work for me, it deletes the c:\test folder as well. I tried on a cmd.exe command line window.

    Thanks!

    dir c:\temp -Directory |Remove-Item -Recurse


    ¯\_(ツ)_/¯

    Thursday, February 20, 2014 4:51 PM
  • rd C:\test /s /q

    md C:\test

    That solution is fine if you don't care about losing any custom permissions set on the base directory.

    I use the following shell script (.cmd) file, emptydir.cmd, that I wrote a long time ago:


    @echo off
    setlocal enableextensions
    if {%1}=={} goto :HELP
    if {%1}=={/?} goto :HELP
    goto :START
    
    :HELP
    echo Usage: %~n0 directory-name
    echo.
    echo Empties the contents of the specified directory,
    echo WITHOUT CONFIRMATION. USE EXTREME CAUTION!
    goto :DONE
    
    :START
    pushd %1 || goto :DONE
    rd /q /s . 2> NUL
    popd
    
    :DONE
    endlocal
    
    

    Bill

    • Proposed as answer by Mike Laughlin Thursday, February 20, 2014 5:52 PM
    Thursday, February 20, 2014 5:21 PM
    Moderator
  • That solution is fine if you don't care about losing any custom permissions set on the base directory.
    Very good point.

    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)

    Thursday, February 20, 2014 5:52 PM
  • Bill did a nice job of hiding the error when using rd .

    I skipped that because I didn't think of hiding it.


    ¯\_(ツ)_/¯

    Thursday, February 20, 2014 6:00 PM
  • You are absolutely right. Actually, I or the script don't have the permission to delete the base directory....
    • Edited by mail8mz Thursday, February 20, 2014 7:12 PM
    Thursday, February 20, 2014 7:11 PM
  • You are absolutely right. Actually, I or the script don't have the permission to delete the base directory....

    If you don't have delete permission then my first example would have worked but it throws an error when you try to delete the root.  Bill's code throws nearly the same error but he has hidden the error.

    Bill's method uses the  fact that you cannot delete the current folder.

    My first example was the one that worked minus hiding the error:

    You can do this:

    rd c:\test /s /q 2>NUL

    If you do have permissions then

    cd \test

    rd \test /s/q 2>NUL

    Bill uses push/pop which is convenient


    ¯\_(ツ)_/¯

    Thursday, February 20, 2014 7:48 PM