'Batch file nested for loop throws "| was unexpected at this time" [duplicate]

I am attempting to store the return of a dir command into a variable. So far all I've found requires a for /f loop. This call is made from within another for /f loop and I am having issues with it. Here are snippets for the relevant parts of the code:

set "list=list.txt"
setlocal enabledelayedexpansion

for /f "tokens=*" %%D in (%list%) do (
     set "path_to=%%D"
     ...
     ...
     ...
     for /f "delims=" %%Z in ('dir /b "%path_to%\..\..\.." | find /c /v ""') do (echo %%Z)
)

I am using %path_to% instead of !path_to! because the variable is outside of this second for loop.



Solution 1:[1]

You have to escape pipe with carret ^| if pipe is used in subject of FOR loop. Pipe is a special character in batch files.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user2956477