Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

EDIT
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
add comment

2 Answers

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
add comment

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.