special characters in for /d do call



Following are a couple of questions about quoting and escaping directory
names which contain special characters (^, &, %).

The following .bat recursively displays the directory names under a root
passed as %1 on the command line.

@echo off

call :dir ^"%%~1^"
for /d %%d in ("%~1\*") do @%0 "%%~d"
goto :eof

echo d: %1 ["%~1"]
goto :eof

1. I've come across the call :dir ^"%%~1^" syntax by mere accident.
Wouldn't have thought it's legal, yet it is the only one which seems to
be passing %~1 correctly to a call'd label. In other words, it works in
the given context (at least under xp.sp3) but I still don't fathom why.

2. I am not able to find a "do call" replacement for the "do @%0" part.
Various attempts of passing the parameter to a call'd label ("%%~d",
^"%%~d^" etc) all fail in one way or another when presented with
directory names which contain special characters (^, &, %). Any hint of
how to make a "do call" work much appreciated.


P.S My test directory structure looks like:

root\amp &1
root\amp &1\amp &&2
root\amp &1\amp &&2\amp &&&3
root\car ^1
root\car ^1\car ^^2
root\car ^1\car ^^2\car ^^^3
root\pct %1
root\pct %1\pct %%2
root\pct %1\pct %%2\pct %%%3


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question