aschipfl
aschipfl

Reputation: 34909

How to escape the `@` variable replacement in `forfiles`?

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:

Upvotes: 3

Views: 507

Answers (2)

jeb
jeb

Reputation: 82257

You could use a delayed expansion

forfiles /P "C:\temp" /M "st*.txt" /C "cmd /v:on /C set at=@& echo !at!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"

Another option is to insert carriage returns, which are removed before output.

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

Upvotes: 3

aschipfl
aschipfl

Reputation: 34909

I just found an even easier method:

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

Note the ^ after the @ sign.

Upvotes: 2

Related Questions