As an added explanation, you need to be aware of Command Prompt's interpretation.
When you have (btw
"example=%%h" should be
set "example=%%h")
Code: Select all
for /f %%h in ('dir /b /o:d | findstr /i ".txt .doc .dat" ') do ("example=%%h" & echo !example!>>tmp)
Command Prompt sees up to the pipe
It tries to execute that and pipe it into
Code: Select all
findstr /i ".txt .doc .dat" ') do ("example=%%h" & echo !example!>>tmp)
It might seem strange because the
do part doesn't act like that, but understand that Command Prompt is sending
('dir /b /o:d | findstr /i ".txt .doc .dat" ') to the
for command to execute, so command characters need to be escaped/deactivated.
That's why
('"dir"') in
for works, while
"dir" itself doesn't. Beware of when to use the escape caret
^ versus quotation marks. For example, red = exposed (command characters are interpreted), blue = safe text (command characters are not interpreted):
Bad:
('dir /b /o:d | findstr /i ".txt .doc .dat" ')Better:
('"dir /b /o:d | findstr /i ".txt .doc .dat""')Best (in this case):
('dir /b /o:d ^| findstr /i ".txt .doc .dat" ')