27
votes

In this command:

FOR /F %%A IN ('TYPE "%InFile%"^|find /v /c ""')DO SET "Till=%%A"

what does the ^ mean?

4
It can also be an escape character although I am not sure about its usage here. robvanderwoude.com/escapechars.phpMike Cheel

4 Answers

25
votes

The ^ symbol (also called caret or circumflex) is an escape character in Batch script. When it is used, the next character is interpreted as an ordinary character.

In your script, the output of the TYPE command to be written as the input to the FIND command.
If you don't use the escape character ^ before the |, then the regular meaning of | is a pipe character.

The Documentation says:

To display a pipe (|) or redirection character (< or >) when you are using echo, use a caret character immediately before the pipe or redirection character (for example, ^>, ^<, or ^| ). If you need to use the caret character (^), type two (^^).

21
votes

The caret '^' character serves two purposes in Windows batch files:

1. line continuations:

~~~

@echo off

dir ^
/ad ^
c:\temp

~~~~

results in dir /ad c:\temp, which lists only the directories in C:\temp.

2. Escaping reserved shell characters & | ( < > ^.

Use a preceding caret to escape and print the character:

echo this pipe will print ^| but this one won't |
echo and this will print one caret ^^
12
votes

Infinite Recursion describes the general behavior of ^, but is a bit fuzzy on why it must be used within IN ('yourCommand').

yourCommand is actually executed implicitly within its own CMD.EXE process using C:\Windows\system32\cmd.exe /c yourCommand. Obviously the pipe must be included in the command in your case. But the entire line must be parsed by the batch parser before it can pass the IN() clause on to be executed. Without the ^, the | confuses the batch parser. The ^ escapes (protects) the pipe during the initial batch parsing.

1
votes

In the given example the caret is used for escaping the special symbol. Though it has other usages in the windows commands

1] In set it is a XOR operator:

set /a "_num=5^3"    &::0101 XOR 0011 = 0110 (decimal 6)

2] In findstr is used for regular expressions of finding string at the beginning of a line:

Echo 12G6 |FindStr /R "[^0-9]"  &::this will check for non-numeric characters