I am using forfiles /S to iterate over a directory tree. I'm running it directly in the command prompt.
To find out the current directory I want to expand the built-in variable %CD%, but not immediately (showing the current directory of the command prompt instance that executes forfiles), but with % escaped in a way that %CD% is expanded by the command prompt instance in the body of forfiles.
forfiles /S /P "D:\Data" /M "*.txt" /C "cmd /C echo %CD%"
Is there a way to escape % and so to kind of hide it from the main command interpreter instance?
I tried %% (that works fine within a batch file), ^%, \%, and even enclosing %CD% in plain and escaped quotes ("%CD%"/\"%CD%\"), but I had no success so far.
I know I could use delayed expansion like the following, but I want to avoid it:
forfiles /S /P "D:\Data" /M "*.txt" /C "cmd /V:ON /C echo !CD!"