Process one line of output only?...

  • Thread starter Thread starter NayJo
  • Start date Start date
N

NayJo

When trying to use the for command to compare 2 files inline I only need to
know that the files match or that they don't. Since 'fc' does not set
errorlevel I compare the output of fc to see if I get the message they
matched. If the files don't match I get many lines of data that I don't
necessarily need.

I tried putting a goto in the unmatch condition but this is making the
cmd.exe complain about mis-matched parentheses. Below is the code I am
trying to use. How can I modify this to succeed on match and fail on
mismatch without comparing every difference in the file?

for /F "skip=1 delims=* usebackq" %%L in (`fc /b reference_queries.txt
example_queries.txt`) do (
if "%%L"=="FC: no differences encountered" (
echo Settings are properly configured.
) ELSE (
REM jumpout as it is enough for me to know the files don't match without
processing every difference.
echo Line is %%L
goto jumpout )
)
:jumpout

This yields ") not expected" error but if I remove the goto, the batch runs
just fine.
 
When trying to use the for command to compare 2 files inline I only need
to
know that the files match or that they don't. Since 'fc' does not set
errorlevel I compare the output of fc to see if I get the message they
matched. If the files don't match I get many lines of data that I don't
necessarily need.

I tried putting a goto in the unmatch condition but this is making the
cmd.exe complain about mis-matched parentheses. Below is the code I am
trying to use. How can I modify this to succeed on match and fail on
mismatch without comparing every difference in the file?

for /F "skip=1 delims=* usebackq" %%L in (`fc /b reference_queries.txt
example_queries.txt`) do (
if "%%L"=="FC: no differences encountered" (
echo Settings are properly configured.
) ELSE (
REM jumpout as it is enough for me to know the files don't match
without
processing every difference.
echo Line is %%L
goto jumpout )
)
:jumpout

This yields ") not expected" error but if I remove the goto, the batch
runs
just fine.

If I'm reading what you want correctly, replace "goto jumpout" with "goto
:eof" and just delete the :jumpout label entirely

HTH

Matt
 
NayJo said:
When trying to use the for command to compare 2 files inline I only need
to
know that the files match or that they don't. Since 'fc' does not set
errorlevel I compare the output of fc to see if I get the message they
matched. If the files don't match I get many lines of data that I don't
necessarily need.

I tried putting a goto in the unmatch condition but this is making the
cmd.exe complain about mis-matched parentheses. Below is the code I am
trying to use. How can I modify this to succeed on match and fail on
mismatch without comparing every difference in the file?

for /F "skip=1 delims=* usebackq" %%L in (`fc /b reference_queries.txt
example_queries.txt`) do (
if "%%L"=="FC: no differences encountered" (
echo Settings are properly configured.
) ELSE (
REM jumpout as it is enough for me to know the files don't match
without
processing every difference.
echo Line is %%L
goto jumpout )
)
:jumpout

This yields ") not expected" error but if I remove the goto, the batch
runs
just fine.

Hi,

On my computer fc does in fact return errorlevel .. so this works for me

fc /b reference_queries.txt example_queries.txt >nul 2>&1 && echo No
differences encountered && goto :EOF

Jens
 
NayJo said:
When trying to use the for command to compare 2 files inline I only need to
know that the files match or that they don't. Since 'fc' does not set
errorlevel I compare the output of fc to see if I get the message they
matched. If the files don't match I get many lines of data that I don't
necessarily need.

I tried putting a goto in the unmatch condition but this is making the
cmd.exe complain about mis-matched parentheses. Below is the code I am
trying to use. How can I modify this to succeed on match and fail on
mismatch without comparing every difference in the file?

for /F "skip=1 delims=* usebackq" %%L in (`fc /b reference_queries.txt
example_queries.txt`) do (
if "%%L"=="FC: no differences encountered" (
echo Settings are properly configured.
) ELSE (
REM jumpout as it is enough for me to know the files don't match without
processing every difference.
echo Line is %%L
goto jumpout )
)
:jumpout

This yields ") not expected" error but if I remove the goto, the batch runs
just fine.

Why not just search the output of fc like this:

fc /b reference_queries.txt example_queries.txt | find /i "no
differences"
if errorlevel 1 (
echo/files differ
) else (
echo/files are the same
)

/Al
 
Matt Williamson said:
If I'm reading what you want correctly, replace "goto jumpout" with "goto
:eof" and just delete the :jumpout label entirely

HTH

Matt
Thank you Matt.
I tried again on my system. You are correct, errorlevel is set if one of
the files does not exist but if both files exist the errorlevel returned is
0 whether or not the files match, at least on my PC.
 
Al Dunbar said:
Why not just search the output of fc like this:

fc /b reference_queries.txt example_queries.txt | find /i "no
differences"
if errorlevel 1 (
echo/files differ
) else (
echo/files are the same
)

/Al

Thank you very much. That works for me with the addition of:

fc /b reference_queries.txt example_queries.txt | find /i "no differences"

to suppress the output of "no differences" to the command line if the
comparison succeeds. This is the fix I will go with.

Thank you also Jens for the goto EOF solution. I haven't tried it since I
need to perform several of these comparisons in my batch file but in
conjunction with 'call sub' it may be a good option.

John C
 
Thank you also Jens for the goto EOF solution. I haven't tried it since I
need to perform several of these comparisons in my batch file but in
conjunction with 'call sub' it may be a good option.

John C
Hi John

Just an idea on how to go about it if you need to perform at lot of
comparisons
.... wrapping the fc stuff into a "sub" and calling it with the two files as
the first two parameters and the label you want to jump to if the two files
differs as the third

... if i understand you correctly its when the files differs that you have to
take some kind of action? .. otherwise change the the two || to && to test
for no differences

Jens

@echo off
call :FILECOMP "file1.txt" "file2.txt" SOMELABEL
call :FILECOMP "file3.txt" "file4.txt" SOMEOTLABEL
:: fill in more lines with files to compare
goto :EOF

:FILECOMP
fc /b %~1 %~2 >nul 2>&1 || echo %~1 and %~2 differs && goto :%3
goto :EOF

:SOMELABEL
echo Do something here
goto :EOF

:SOMEOTLABEL
echo DO something different here
goto :EOF
 
Jens Hastrup said:
Hi John

Just an idea on how to go about it if you need to perform at lot of
comparisons
... wrapping the fc stuff into a "sub" and calling it with the two files as
the first two parameters and the label you want to jump to if the two files
differs as the third

.. if i understand you correctly its when the files differs that you have to
take some kind of action? .. otherwise change the the two || to && to test
for no differences

Jens

@echo off
call :FILECOMP "file1.txt" "file2.txt" SOMELABEL
call :FILECOMP "file3.txt" "file4.txt" SOMEOTLABEL
:: fill in more lines with files to compare
goto :EOF

:FILECOMP
fc /b %~1 %~2 >nul 2>&1 || echo %~1 and %~2 differs && goto :%3
goto :EOF

:SOMELABEL
echo Do something here
goto :EOF

:SOMEOTLABEL
echo DO something different here
goto :EOF

Thank you Jens. That works. I had to research || and && to see what they
do. I did not know those were available.

And of course I meant 'goto label' but was thinking subroutine.

Many thanks.

John C.
 
NayJo said:
Thank you Jens. That works. I had to research || and && to see what they
do. I did not know those were available.

And of course I meant 'goto label' but was thinking subroutine.

Many thanks.

John C.
Hi John,

Thanks for the thanks - i'm glad you can use it :-)

Jens
 
Back
Top