0

That is the identical content of my 2 txt files formatliste_droid.txt and formatliste_siegfried.txt. I want to know if all the lines till delimiter $:$ are identical 1-1, 2-2 and so on.

Z:\15_Testdateien\jhove_Script_fuer_MIX-Verzeichnisse\testfiles\BVBBV042064787\Files\E\images.jpg$;$fmt/43
Z:\15_Testdateien\jhove_Script_fuer_MIX-Verzeichnisse\testfiles\BVBBV042064787\Files\E\Van Schijndel House - English.mp4$;$fmt/199
Z:\15_Testdateien\jhove_Script_fuer_MIX-Verzeichnisse\testfiles\BVBBV042064787\Files\E\Van Schijndelhuis - Nederlands.mp4$;$fmt/199

That's my code:

@echo off
Setlocal EnableDelayedExpansion 
for /f "tokens=1 delims=$;$" %%a in (formatliste_siegfried.txt) do ( 
for /f "tokens=1 delims=$;$" %%c in (formatliste_droid.txt) do (                                      
if /i  %%a==%%c (
echo  match
pause
)

)
)

The problem is that I always get a match even if the appropriate lines are not identical

Oleg_08
  • 447
  • 1
  • 4
  • 23
  • Surely the separator is the semicolon, **;** or at least the sequence, **$;$**. When using `Delims` you should however, in this case just need `"Delims=$"` – Compo Jul 20 '18 at 15:52
  • 2
    first: `delims` are one-character-delimiters. `delims=$;$` delimits at each `$` and at each `;`. Second: you compare *each line* of "Siegfried" with *each line* of `droid`. Is that your intention? Or do you want to compare each line of "Siegfried" with the corresponding line of "droid" (same line number)? (for debugging you should use `echo match: %%a # %%c`) – Stephan Jul 20 '18 at 15:55
  • Might I suggest you just use `%%a` with `Find`, i.e. `Do Find /I "%%a$;$"<"formatliste_droid.txt">Nul&&(Echo [match] %%a)||Echo [unique] %%a` – Compo Jul 20 '18 at 15:58
  • 2
    Possible duplicate of [How can two text files be read in parallel by a batch file?](https://stackoverflow.com/questions/38214874/how-can-two-text-files-be-read-in-parallel-by-a-batch-file) –  Jul 20 '18 at 16:57
  • @Stephan I want to compare each line of Siegfired with corresponding line of Droid. I have the same problem if I limit the delimiter to only 1 symbol – Oleg_08 Jul 23 '18 at 16:01
  • you should even get some error messages. Change the `if` syntax to `if /i "%%a"=="%%c"` to avoid them (it's the spaces, which cause the syntax errors - see [explanation](https://stackoverflow.com/a/48282847/2152082)). Combine it with LotPings link to read two files simultanely. – Stephan Jul 23 '18 at 16:15

1 Answers1

0

TL;DR -

Use the fc.exe command.

Details -

There are a few issues with this logic preventing you from getting the output you're looking for. The reason you only get "match" for output is because that is the only possible output - there's only one "echo" line. Adding a case else would output the number of times a line doesn't match.

That said, adding the else would be too noisy to be valuable since the large majority of the output would be "doesn't match". The logic here says "for each line of file 1, check every single line of file 2 to check for a match". For example: given 2 files which each have 100 lines, even if each line had a match in the other file, for each of the 100 outer loops, the output would be 1 "match" and 99 "doesn't match". I'll assume you don't want 100 "match" and 9900 "doesn't match".

So instead, and still considering the question being "how to compare the text lines from two files", I'd recommend using the FC.exe command. Whether batch or vanilla command line, it was designed to compare files.

Other items for consideration:

  • Missing double quotes around comparison items containing spaces

    %%a==%%c ... should be... "%%a"=="%%c"
  • "delims=" can match nearly any single character, but never a contiguous multi-character group

  • "Setlocal EnableDelayedExpansion" isn't required as there's no dynamic use of "set"
  • if case is important, the use of "if /i" will break the integrity of the check