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

Posted on 2012-08-21
Medium Priority
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
  • 2
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 38317443
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 85

Accepted Solution

oBdA earned 2000 total points
ID: 38317466
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


Author Comment

ID: 38317663
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 85

Expert Comment

ID: 38317729
("%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, ...)

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month16 days, 21 hours left to enroll

864 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