Using RMDIR with FOR statement fails.... directories with spaces

Posted on 2012-08-21
Last Modified: 2012-08-21
Using the following batch script, I'm trying to remove old subdirectories. This snippet works perfectly when I use it in circumstances where there are no spaces in the directory names... However, when there are spaces in the directories, i have problems such as:

C:\Program Files\My Company Apps>rmdir /s /q "C:\Program Files\My Company Apps"\C:\Program
The filename, directory name, or volume label syntax is incorrect.

Here is the batch code I'm tinkering with....

IF EXIST "C:\Program Files\My Company Apps" (
         SET App32dir=C:\Program Files\My Company Apps
       pushd "%App32dir%"
         dir /b /ad "%App32dir%" > "%App32dir%\dirlist.txt"
         for /f %%i in ("%App32dir%\dirlist.txt") do rmdir /s /q "%App32dir%"\%%i
  ) ELSE (
         ECHO :  My Company Apps Not Found

I have tried every combination I can think of..

"%App32dir%\%%i" and "%App32dir%"\"%%i"

I have also tried only using quotes in the SET command, then removing them from the variable %App32dir% but still to no avail....

The dirlist.txt populates correctly with the subdirectories names...

All have spaces, like:

My First App
My Second App
My Third App
My Fourth App
And So On

So, the dirlist.txt file looks good, but since the directories have spaces, the rmdir command doesn't work..

Question by:zequestioner
    LVL 37

    Expert Comment

    by:Gerwin Jansen
    remove the 2nd appdir variable on your remove command line leaving double quotes around the variable

    for testing, add an echo command before the rmdir, that way you can debug easier

    so: ... echo rmdir /s /q "%%i"
    LVL 82

    Accepted Solution

    Try this; it's in test mode and will only show the commands it would normally run; remove the uppercase ECHO in front of "rmdir" to run it for real:
    @echo off
    SET App32dir=C:\Program Files\My Company Apps
    IF EXIST "%App32Dir%" (
    	for /d %%i in ("%App32dir%\*.*") do ECHO rmdir /s /q "%%i"
    ) ELSE (
    	echo :  My Company Apps Not Found

    Open in new window

    LVL 1

    Author Comment

    Thanks for the quick response!

    Gerwin, your solution only wrapped the first word of each directory name in quotes. (in dirlist.txt) so did not work.

    oBdA, your solution worked great, however I had to change ("%App32dir%\*.*") to ("%App32dir%"\*.*)

    It then deleted all subdirectories, except for one which gave an error stating 'The directory is not empty'

    Any suggestions on that?

    Otherwise, thanks for your help!
    LVL 82

    Expert Comment

    ("%App32dir%\*.*") should work just fine; it does here and has been doing so in hundreds of scripts. Could it be that the line "SET App32dir=C:\Program Files\My Company Apps" has a space at the end?
    The remaining folder in question has at least one file in it that couldn't be deleted; not much you can about except checking why it can't be deleted (permissions, in use, read only, ...)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
    You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now