Home > Net >  Combination of ^< characters in Batch
Combination of ^< characters in Batch

Time:09-15

I'd like to know what does ^< character combination means in my case. I already know that ^< helps to interpret < as an ordinary character. But what does it mean here: for /F %%i in ('^< !FILE! find /C /V ""') do set count=%%i

I suppose this code counts lines in a particular file but I'm not sure about these characters.

Thanks, I appreciate all your help!

CodePudding user response:

for /F %%i in ('^< !FILE! find /C /V ""') do set count=%%i

you are right, the for loop populates the variable %count% with the number of lines in a file.

The "usual" usage of find is find /c /v "" file.txt (try it and note the output - not a good output to get the number only).
Therefore find gets its input from STDIN: <file.txt find /c /v "" (again, try it and note the output - much better for capturing).
The < has to be escaped (with a ^) to not ruin the syntax of the for loop.

The part in parentheses runs in a separate instance of cmd and you need the < in that instance, not in the original instance. Escaping forces the < to be treated "as ordinary character" in the original instance. The escape char is removed with the first parsing, sending the plain (unescaped) < to the second instance, where it does what it should do. - redirecting the contents of the file to STDIN.

You could also do the same thing by using doublequotes instead of the caret escape character (Thank you @Compo for the suggestion):

for /F %%i in ('"< !FILE! find /C /V """') do set count=%%i

Strings in quotes are parsed less strictly. In case you are interested in a more in-depth explanation - be warned - it's hard stuff...

  • Related