Page 1 of 1
circumvent findstr white space eol
Posted: 28 May 2011 23:36
by tebee
Hi,
in the following example
Code: Select all
for /f %%h in ('dir /b /o:d | findstr /i ".txt .doc .dat" ') do ("example=%%h" & echo !example!>>tmp)
fails if filename has an inner white space, i.e, "Copy of blabla.txt" is redirected as "Copy".
Otherwise, if the command part of <for in do> is executed alone on the command line the the output is correct.
How to get the proper output?
Thanks
Re: circumvent findstr white space eol
Posted: 28 May 2011 23:48
by amel27
Code: Select all
@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%h in ('dir/b/o:d^|findstr/i ".txt .doc .dat"') do (
set "example=%%h"
echo !example!>>tmp
)
Re: circumvent findstr white space eol
Posted: 29 May 2011 16:37
by orange_batch
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" ')
Re: circumvent findstr white space eol
Posted: 29 May 2011 19:51
by amel27
orange_batch wrote:Best (in this case):
('dir /b /o:d ^| findstr /i ".txt .doc .dat" ')
the best is:
Code: Select all
('dir/b/o:d^|findstr/i "\.txt \.doc \.dat" ')
;)
Re: circumvent findstr white space eol
Posted: 29 May 2011 23:20
by tebee
Thanks to orange_batch for picking typo, of course it did not work the way i wrote; my testing was made on (literaly):
for /f "delims=" %%j in ('dir /b /o:d ^| findstr /I "%ftype%"') do (set /a Litems+=1 & echo %%j>>!flist!)
with just "delims=" missing. Now is fine, after adding the missing part.
When i say the command executed alone is ok, it was:
Code: Select all
dir /b /o:d | findstr /I ".txt .doc .dat"
and it matches with the missing "delims=" in code.
What i would like to understand better the reason of various escapes used, i.e. amel27 suggest using a backlash preceding any atom of search string, "\.txt" instead of ".txt".
Actually i see continuously escapes inserted in code and i could not figure if there is a collection or a book that could teach how to use them. Can someone give the reference?
Thanks to all.
Re: circumvent findstr white space eol
Posted: 29 May 2011 23:51
by orange_batch
Within
for's file-set/string/command parentheses is the only situation where your intention is to use the | character as a pipe, but have to escape it to plain text. The same goes for & symbols within. All other cases should be normal. For example:
Tries to pipe echo:
Actually echoes |: