3

I am using a forfiles command line like this (Windows 7 x64):

forfiles /P "C:\root" /M "*.txt" /C "cmd /C echo @relpath"

How can I escape the replacement of @relpath (relative path to currently iterated item) to get @relpath output literally? (...or any other @ variable?)


So far I tried the following things, without success:

  • \: the @ seems to be handled before \ so \@relpath does not work, rather the expanded output is just preceded with \;
  • ^: stating ^@ does not show any effect except that the ^ disappears; writing ^^@ does not help eigher, one ^ remains in the expanded output then;
  • 0xHH: surprisingly (to me), the forfiles-specific replacement of hexacecimal numbers 0x40 does not work either, it seems that this is done prior to variable parsing;
  • @@: doubling the @ keeps the first @ literally, that is it;
aschipfl
  • 33,626
  • 12
  • 54
  • 99

2 Answers2

3

You could use a delayed expansion

forfiles /P "C:\temp" /M "st*.txt" /C "cmd /v:on /C set r=0x40 & echo !r:~0,1!relpath"

Or you can use percent expansion.

set percent=%
set at=@
forfiles /P "C:\temp" /M "st*.txt" /C "cmd /C echo %percent%at%percent%relpath"
jeb
  • 78,592
  • 17
  • 171
  • 225
  • Great approach! in the first option, if you omit the space after `set r=0x40` or if you enclose it in `()`, a simple expansion like `!r!` works as well... – aschipfl Sep 04 '15 at 17:13
2

I just found an even easier method:

forfiles /P "C:\root" /M "*.txt" /C "cmd /C echo @^relpath"

Note the ^ after the @ sign.

aschipfl
  • 33,626
  • 12
  • 54
  • 99