• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1196
  • Last Modified:

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

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
       popd
  ) ELSE (
         ECHO :  My Company Apps Not Found
)
pause

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

Suggestions?
0
zequestioner
Asked:
zequestioner
  • 2
1 Solution
 
Gerwin Jansen, EE MVETopic Advisor Commented:
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"
0
 
oBdACommented:
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
setlocal
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
)
pause

Open in new window

0
 
zequestionerAuthor Commented:
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!
0
 
oBdACommented:
("%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, ...)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now