findstr fails under certain circumstances

Sep 24, 2008
3
0
#1
The following line fails when run in tcc/le but not when run in cmd.exe (it is part of a batch file)

for /F %%v in ('echo %1^|findstr "^start$ ^stop$ ^restart$ ^install$ ^remove$ ^query$ ^ping$ ^setup"') do call :exec set COMMAND=%%v

The errors I get is:
FINDSTR: Cannot open ping$
FINDSTR: Cannot open etup >! C:\DOCUME~1\mortenk\LOCALS~1\Temp\FOR252.tmp


If I remove two of the "targets" (e.g. ^remove$ and ^query$) findstr works as intended.


Thanks for a great product
Morten
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,504
46
Albuquerque, NM
prospero.unm.edu
#2
I haven't tried it, but I suspect that what you're seeing is not a FINDSTR problem per se, but differences in how the escape character is used. You might try disabling escape char processing via SETDOS /X-8 before the problematic line (and re-enabling it with SETDOS /X0 afterwards), or perhaps using SETDOS /E to change the escape character to something more obscure.
 
Sep 24, 2008
3
0
#3
Thanks for the quick reply

I have tried SETDOS /X-8 and that results in the error message not being displayed.

When doing this the script however stops working.

Hope we can fix this somehow since I would like to be cmd-independent :)
 

rconn

Administrator
Staff member
May 14, 2008
10,325
94
#4
MLK wrote:

> The following line fails when run in tcc/le but not when run in cmd.exe
> (it is part of a batch file)
>
> for /F %%v in ('echo %1^|findstr "^start$ ^stop$ ^restart$ ^install$
> ^remove$ ^query$ ^ping$ ^setup"') do call :exec set COMMAND=%%v
>
> The errors I get is:
> FINDSTR: Cannot open ping$
> FINDSTR: Cannot open etup >! C:\DOCUME~1\mortenk\LOCALS~1\Temp\FOR252.tmp
>
> If I remove two of the "targets" (e.g. ^remove$ and ^query$) findstr
> works as intended.
That line by itself won't work in either CMD or TCC. (What is the %1
argument? What is the "exec" call? What is the "set" command doing in
the CALL statement? Why are you using the escape character (^) in front
of all of the commands?)

Can you post a minimal batch batch file that works in CMD but fails in TCC?

Rex Conn
JP Software
 
#5
On Thu, 25 Sep 2008 07:18:36 -0500, "JP Software Forums" <[email protected]>,rconn
<> wrote:


>Can you post a minimal batch batch file that works in CMD but fails in TCC?
I don't know it this addresses all the OP's concerns, but one thing is the same
at the TCC command line. Giving FINDSTR one additional string to look for
(^foo$) causes the command to fail:

v:\> for /F %v in ('echo start | findstr "^start$ ^stop$ ^restart$ ^install$
^remove$ ^query$"') echo %v
start

v:\> for /F %v in ('echo start | findstr "^start$ ^stop$ ^restart$ ^install$
^remove$ ^query$ ^foo$"') echo %v
FINDSTR: Cannot open ?oo$ >! e:\temp\FORD60.tmp

**NOTE**: In that error message a "female sign" precedes "oo$".

Either command line, appropriately modified for CMD works at the CMD command
line (note, echo is ON).

v:\> for /F %v in ('echo start ^| findstr "^start$ ^stop$
^restart$ ^install$ ^remove$ ^query$"') do echo %v

v:\> echo start
start

v:\> for /F %v in ('echo start ^| findstr "^start$ ^stop$
^restart$ ^install$ ^remove$ ^query$ ^foo$"') do echo %v

v:\> echo start
start
 
#7
JP Software Forums" <[email protected]>; "Charles Dye wrote:
|
**NOTE**: In that error message a "female sign"
| precedes "oo$".
|
| That 'female sign' is most likely the form-feed character, Ctrl-L --
| generated in TCC by ^F.

... which may be the reason _this_ particular string causes failure. Its
introductory character is considered to be an EscapeChar, and instead of the
two-character sequence "caret f" (^f) the FF character is passed to FINDSTR.

Using SETDOS /E or OPTION //EscapeChar= to change the escape character from
TCC default should cure the OP's problem.
--
Steve
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,504
46
Albuquerque, NM
prospero.unm.edu
#8
"Charles Dye wrote:
|
|
| That 'female sign' is most likely the form-feed character, Ctrl-L --
| generated in TCC by ^F.

... which may be the reason _this_ particular string causes failure. Its
introductory character is considered to be an EscapeChar, and instead of the
two-character sequence "caret f" (^f) the FF character is passed to FINDSTR.
Well, that and its position. Note that the preceding escape sequence (from "^query$") is ^Q, the double quote -- prematurely closing the quoted string. If you were to rearrange the search terms so that ^query$ was earlier in the list, I'll bet you'd see more problems.
 
May 30, 2008
42
0
#9
From: JP Software Forums
Sent: Thursday, September 25, 2008 10:46 AM
Subject: RE: [Support-t-493] Re: findstr fails under certain circumstances
[snip]

> Well, that and its position. Note that the preceding escape sequence
> (from "^query$") is ^Q, the double quote -- prematurely closing the
> quoted string. If you were to rearrange the search terms so that ^query$
> was earlier in the list, I'll bet you'd see more problems.
I must admit, I'm somewhat concerned by the facts that this is actually able
to expose an internal implementation detail of FOR, and that that detail
involves writing the output of the command to a temporary file. =/ These are
both somewhat disconcerting to me.

Jonathan Gilbert
 
#10
JP Software Forums" <[email protected]>; "logic wrote:
| From: JP Software Forums
| Sent: Thursday, September 25, 2008 10:46 AM
| Subject: RE: [Support-t-493] Re: findstr fails under certain
| circumstances [snip]
|
|
> Well, that and its position. Note that the preceding escape
| sequence
|| (from "^query$") is ^Q, the double quote -- prematurely closing the
|| quoted string. If you were to rearrange the search terms so that
|| ^query$ was earlier in the list, I'll bet you'd see more problems.
|
|
| I must admit, I'm somewhat concerned by the facts that this is
| actually able to expose an internal implementation detail of FOR, and
| that that detail involves writing the output of the command to a
| temporary file. =/ These are both somewhat disconcerting to me.

No, there is nothing related to FOR implementation here. The issue is that
in 4NT and TCC the caret ^ character is the default for the command-line
EscapeChar, which combines with the immediately subsequent command line
character into a possibly inappropriate (for FINDSTR, that is) special
character, e.g., ^f is converted into FF, ^q into ", etc. The issue actually
exposed is that using the caret ^ as the default EscapeChar may be
incompatible with FINDSTR syntax.

I don't think the FOR created a temporary file, either. Neither issue that
you considered "disconcerting" has the property you did not consider best.
--
Steve
 
May 30, 2008
42
0
#11
From: JP Software Forums
Sent: Thursday, September 25, 2008 9:43 PM
Subject: RE: [Support-t-493] Re: findstr fails under certain circumstances

>
> No, there is nothing related to FOR implementation here. The issue is
> that in 4NT and TCC the caret ^ character is the default for the command-
> line EscapeChar, which combines with the immediately subsequent command
> line character into a possibly inappropriate (for FINDSTR, that is)
> special character, e.g., ^f is converted into FF, ^q into ", etc. The
> issue actually exposed is that using the caret ^ as the default
> EscapeChar may be incompatible with FINDSTR syntax.
>
> I don't think the FOR created a temporary file, either. Neither issue
> that you considered "disconcerting" has the property you did not consider
> best.
Uh, sorry?

This is the original error message posted by MLK:


> The errors I get is:
> FINDSTR: Cannot open ping$
> FINDSTR: Cannot open etup >! C:\DOCUME~1\mortenk\LOCALS~1\Temp\FOR252.tmp
Jonathan Gilbert
 
Sep 24, 2008
3
0
#12
The original line I posted is from the Hyperic HQ 4.0 Beta "hq-agent.bat".
(Sorry for not posting the line I made work in cmd but not in tcc)


I have checked whether a file was created, which was not the case.


The line I used for testing was this one:

for /F %v in ('echo ping^|findstr "^start$ ^stop$ ^restart$ ^install$ ^remove$ ^query$ ^ping$ ^setup"') do echo %v

which works in both cmd and fails in tcc.



To test the "^q=double quotes" I then tried removing the ^query$ only and adding a few more targets and I get this (in TCC):

[C:\]for /F %v in ('echo ping^|findstr "^start$ ^stop$ ^restart$ ^install$ ^remove$ ^ping$ ^setup ^bla ^bla2"') do echo %v
ping




When isolating the findstr like this:
echo ping|findstr "^start$ ^stop$ ^restart$ ^install$ ^remove$ ^query$ ^ping$ ^setup"

which works in tcc and cmd.



So somehow when "findstr" is wrapped in the "for" it fails. Could it perhaps be the single and doublequotes wrapping findstr?

Morten
 
May 30, 2008
42
0
#13
From: JP Software Forums [mailto:[email protected]]
Sent: Saturday, September 27, 2008 2:40 AM
Subject: RE: [Support-t-493] Re: findstr fails under certain circumstances
[snip]

> I have checked whether a file was created, which was not the case.
If you don't believe that a file is created, try this:

[C:\WINDOWS\system32]for /F %i in ('dir *.txt') do echo %i
eula.txt
h323log.txt
MSOracle32Readme.txt
SQLSRDME.TXT
VFPODBC.TXT

[C:\WINDOWS\system32]cacls "%temp" /E /D Everyone
processed dir: C:\DOCUME~1\JONATH~1\LOCALS~1\Temp

[C:\WINDOWS\system32]for /F %i in ('dir *.txt) do echo %i
4NT: (Sys) Access is denied.
"C:\DOCUME~1\JONATH~1\LOCALS~1\Temp"

[C:\WINDOWS\system32]cacls "%temp" /E /R Everyone
processed dir: C:\DOCUME~1\JONATH~1\LOCALS~1\Temp

[C:\WINDOWS\system32]for /F %i in ('dir *.txt') do echo %i
eula.txt
h323log.txt
MSOracle32Readme.txt
SQLSRDME.TXT
VFPODBC.TXT

[C:\WINDOWS\system32]

The first CACLS line adds an ACE to the Temp directory denying everyone
access. The second CACLS line deletes that ACE, restoring the ACL to its
previous state. I think it's pretty clear that FOR /F *does* write a
temporary file. This in and of itself isn't a serious problem, though I
wouldn't do it that way myself, but the way in which it is done is what
disturbs me. 4NT is, as far as I can infer from the error message earlier in
this thread, just tacking ">!" + GetTempFileName() onto the end of the
command-line within (' ') used when recursively invoking the command
processor to get the results. *That* is what disturbs me. =/

Jonathan Gilbert