Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

findstr fails under certain circumstances

Sep
3
0
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
 
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.
 
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 :)
 
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
 
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
 
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 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.
 
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
 
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
 
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
 
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
 
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
 

Similar threads

Back
Top