circumvent findstr white space eol

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tebee
Posts: 13
Joined: 21 May 2011 08:41

circumvent findstr white space eol

#1 Post by tebee » 28 May 2011 23:36

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

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: circumvent findstr white space eol

#2 Post by amel27 » 28 May 2011 23:48

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
)

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: circumvent findstr white space eol

#3 Post by orange_batch » 29 May 2011 16:37

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

Code: Select all

for /f %%h in ('dir /b /o:d |

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" ')

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: circumvent findstr white space eol

#4 Post by amel27 » 29 May 2011 19:51

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" ')
;)

tebee
Posts: 13
Joined: 21 May 2011 08:41

Re: circumvent findstr white space eol

#5 Post by tebee » 29 May 2011 23:20

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.

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: circumvent findstr white space eol

#6 Post by orange_batch » 29 May 2011 23:51

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:

Code: Select all

echo |

Actually echoes |:

Code: Select all

echo ^|

Post Reply