NT4 DOS Batch: Cant get echo pipe findstr to work

gouranga
gouranga used Ask the Experts™
on
I have a batchfile that I've modified from an earlier question.
The batch does an FTP then checks for 5## error codes in the FTP output, this is great unless there's a file that happens to be 5###..  bytes in size as this is also caught as a error:

525 File Not Found
5000 bytes downloaded

The checking part of the script does a ECHO %VAR:~0,3%|FINDSTR /L "5[0-9][0-9]"
So I changed it to:
ECHO %VAR:~0,4%|FINDSTR /L "5[0-9][0-9] "

But it doesn't work!!  The only change is the 0,3 to 0,4 (and a space at the end of the findstr) but it doesn't like it and it says "|" was unexpected!  It works on the command line, but not in the batch!

Heres the code:
FTP -i -n -s:%temp%\ftpscr.dat > c:\temp\ftseftp.out

del %temp%\ftpscr.dat

:: ---------ERROR-CHECKING---------
FOR /f "TOKENS=*" %%i IN (c:\temp\ftseftp.out) DO (set JON=%%i&call :proclog)

IF %ERRCOUNT% GTR 0 (ECHO Error on transmission of files! >> c:\temp\ftseftp.out) ELSE (
echo OK
)
echo ErrorCount is %ERRCOUNT%
set errorlevel=%ERRCOUNT%
GOTO QUIT

:PROCLOG
ECHO %JON:~0,4%|FINDSTR /L "5[0-9][0-9] "
IF NOT ERRORLEVEL 1 (SET /A ERRCOUNT=%ERRCOUNT% + 1)

GOTO :EOF
:: ---------ERROR-CHECKING---------

:QUIT
if  %ERRORLEVEL% == 0 goto :end
echo FAILED
call c:\false.exe
:end
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
I think your solution is incomplete, even if the code would run, see :

525 File Not Found
512 bytes downloaded

And now ?

I would try following :

:PROCLOG
ECHO %JON% | FIND /I "bytes downloaded"
IF NOT ERRORLEVEL 1 GOTO end
ECHO %JON:~0,3%|FINDSTR /L "5[0-9][0-9]"
IF NOT ERRORLEVEL 1 (SET /A ERRCOUNT=%ERRCOUNT% + 1)

GOTO end

if the %JON:~0,3% line still doesn't work then try

FINDSTR /L "5[0-9][0-9]" < ECHO %JON:~0,3%

Author

Commented:
Sorry, I'm not being clear..

The Script reads the FTP output file line by line:
FOR /f "TOKENS=*" %%i IN (c:\temp\ftseftp.out) DO (set JON=%%i&call :proclog)

Then in the proc proclog it checks the first 3 (or in the non-working modified version 4) characters
ECHO %JON:~0,3%     or        ECHO %JON:~0,4%

And pipes that to the findstr command that looks for "5##" or "5## "

In the version that echo's the 4 characters the script fails with:
| was unexpected at this time.

Is there a way of turning on more verbose logging?

Author

Commented:
I've got it through trial and error..

ECHO (%JON:~0,4%)|FINDSTR /L "5[0-9][0-9] "

Putting the echo in brackets protects it from the |
Commented:
>> I've got it through trial and error..

Good. Still, I've warned you about something, see below.

>> Sorry, I'm not being clear..

No, you've been prefectly clear, quite possibly I wasn't myself. My point is that your solution attempt is IMHO wrong. If someday you have a line like

512 bytes downloaded

in your logs, then the script will still report an error where there isn't. So I proposed to check first for the presence of "bytes downloaded" in the JON line and return immediately if found. Once these cases are eliminated, you can use the lines

ECHO %JON:~0,3%|FINDSTR /L "5[0-9][0-9]"
IF NOT ERRORLEVEL 1 (SET /A ERRCOUNT=%ERRCOUNT% + 1)

as usual, without fearing that a "xxxx bytes downloaded" line will interfere.

As for why an error is appearing when you try to increase from 3 to 4 characters, I honestly have no idea. Nor do I know how to get a more verbose debug output.

Last, I simply proposed to circumvent the problem with

FINDSTR /L "5[0-9][0-9]" < ECHO %JON:~0,3%

or

FINDSTR /L "5[0-9][0-9] " < ECHO %JON:~0,4%

Hope this helps.

Author

Commented:
Oh I see..
Thank you..

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial