Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Goal: Delete empty folders at end of a mutli-step clean-up. rd without "Force" removes directories that are not empty.

This works:

for /f "usebackq" %%d in (`"dir O:\Folder~1\Folder /ad/b/s | sort /R"`) do rd "%%d"

These don't work:

for /f "usebackq" %%d in (`"dir \"O:\Folder With Spaces\Folder\" /ad/b/s | sort /R"`) do rd "%%d"
for /f "usebackq" %%d in (`"dir ""O:\Folder With Spaces\Folder"" /ad/b/s | sort /R"`) do rd "%%d"
for /f "usebackq" %%d in (`"dir ^"O:\Folder With Spaces\Folder^" /ad/b/s | sort /R"`) do rd "%%d"

I know I'm missing something simple...

Adding the example below to the mix:
enter image description here

If I actually echo the %d it echos.... "O:\Patient". Not the full name.

share|improve this question
up vote 1 down vote accepted

You don't need the (outer) double quotes with usebackq.

You can get the list of directories from your piped commands, but when the output has spaces, only the first part will go to the %%d variable. To avoid that, use an additional tokens=* parameter (from for /?):

for /f "usebackq tokens=*" %%d in (`dir "C:\Program Files" /ad /b /s ^| sort /R`) do echo "%%d"
share|improve this answer

You need to escape the pipe character:

FOR /F "usebackq" %i IN (`dir /b ^| sort`) DO echo %i
share|improve this answer
Updated question with an example. The pipe isn't an issue (Actually removed it since the sort is kinda unneeded). The issue is the quotes within quotes. – WernerCD Oct 5 '11 at 20:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .