Escape parentheses in nested batch for loop?



I have a folder which contains an unknown number of .txt files named as such:

todo (1).txt
todo (2).txt
todo (3).txt

I’m running this command to iterate through the files and print the text on each line within the files: for /r %f in (test\*.txt) do (for /f %p in (%f) do echo %p)

/r returns the file names correctly and /f will output each line of text in todo.txt but when it hits todo (1).txt it performs the command for /f %p in (C:\test\todo (1).txt) do echo %p and errors out saying The system cannot find the file C:\test\todo.

How do you escape parentheses in this situation?


use usebackq option to allow you to double-quote the filename

for /r %f in (test\*.txt) do (for /f "usebackq" %p in ("%f") do echo %p)

Answered By – npocmaka

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More