Jump to content

FINDSTR workaround needed


Recommended Posts

Hi im using FINDSTR from batch file to compare two text files, however the files contain rather long lines, maybe greater than or around 200 characters.

and when run, FINDSTR simply complains that the search string is "too long", the limit seems to be somwhere around 127.

Is there any workaround or a different way to compare two text files? as far as i know the "FIND" command has a larger limit but im not sure how to make it work to my liking if it is possible at all.

any help is appreciated.

Link to comment
Share on other sites


without using 3rd party tools.

There's nothing forcing you to use any "tools" for this (included or 3rd party). You could move from 1980's best (batch files) to 1990's technology: VBScript (using InStr for example, which is meant precisely for this -- comparing text strings). You can even use regular expressions if you want... And that has worked out of the box on any Windows box for a little over a decade.

Link to comment
Share on other sites

heh, thanks, i dont actually know any VB or anything i just wanted a fast way to compare the files with something i knew a little about which was just basically the batch tools.

can you confirm that findstr will not work? if it has any limitations and if i could somehow work round them?

EDIT: ill give VBscript a try, and see how it goes :)

Edited by bauxite
Link to comment
Share on other sites

Sorry i should have been more specific.

Its just two text files that contain unique lines of characters (including numbers/symbols like :,\)

1.txt contains on each line:

abcdefg

hds8745

fjhjgsjnn4

C:\im random\ini.txt

and 2.txt is almost similar:

Xbcdefg

hds8745

fjhjgsjnn4

C:\im random\ini.txt

im new.txt

so in 2.txt there may be new unique lines and/or some that are modified from 1.txt.

I used findstr like:

findstr /l /v /g:1.txt 2.txt >> changes.txt

which as i understand would try to match each line from 2.txt to 1.txt (not sure what order) and if no match was found redirect it to changes.txt therefore i would get a new text file with all the new or modified lines (although not deleted lines as far as i know).

this works as long as the entire line is less than about 125 characters long, that was the problem.

Edited by bauxite
Link to comment
Share on other sites

I have not tested this, but this VBS script might do what you want.

Fill in the paths

PATH_TO_FILE\SomeFile1.txt

PATH_TO_FILE\SomeFile2.txt

PATH_TO_FILE\Differences.txt

Save As Sort.vbs

 Dim Fso :Set Fso = Createect("Scripting.FileSystemect")
Dim StrTxt1, StrTxt2, StrTxt3, Ts
'-> First Text File
Set Ts = Fso.OpenTextFile("PATH_TO_FILE\SomeFile1.txt", 1)
StrTxt1 = Ts.ReadAll
Ts.Close
'-> Second Text File
Set Ts = Fso.OpenTextFile("PATH_TO_FILE\SomeFile2.txt", 1)
Do Until Ts.AtEndOfStream
StrTxt2 = Ts.ReadLine
'-> If Not There Add It
If Not InStr(StrTxt2, StrTxt1) Then
StrTxt3 = StrTxt3 & StrTxt2 & vbCrLf
End If
Loop
Ts.Close
'-> Third Text File
Set Ts = Fso.CreateTextFile("PATH_TO_FILE\Differences.txt")
Ts.WriteLine StrTxt3
Ts.Close

Link to comment
Share on other sites

Dim Fso :Set Fso = CreateObject("SScripting.FileSystemObject")

or perhaps:

Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")

(no double S) ;)

edit: looks like you fixed it already...

Either ways, I'm still not completely sure how it should work in the first place, like how it should react if extra lines were inserted in one file (report the extra line, or keep reporting mismatches from that point on), or if it should be a "dumb" line by line comparison against the other file, or even just seeing if the line merely exists in the other file (line/order unimportant). We need more details.

Edited by CoffeeFiend
Link to comment
Share on other sites

Thank you all, the script now runs however it just gives me a copy of "SomeFile2.txt" in "Differences.txt",

im not sure what you mean, the files are not being changed while the script is run? ill try to explain it better:

Every line in the text file is unique to that particular file, and both files contain some lines which match exactly and others which do not, however either file may have more or less lines the the other.

simply i want to discard all the same lines to leave me with the rest or, only the lines in "2.txt" which are not in "1.txt" (if that makes the process easier)

Hope im making sense, i think i may even be intersted in learning VBscript, although i barely have much time and it seems rather difficult, anyway thanks :)

after quite a bit of reading at the MSDN site, specifically on the InStr function, and with lots of luck i seemingly found a solution.


Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
Dim StrTxt1, StrTxt2, StrTxt3, Ts
'-> First Text File
Set Ts = Fso.OpenTextFile("PATH_TO_FILE\SomeFile1.txt", 1)
StrTxt1 = Ts.ReadAll
Ts.Close
'-> Second Text File
Set Ts = Fso.OpenTextFile("PATH_TO_FILE\SomeFile2.txt", 1)
Do Until Ts.AtEndOfStream
StrTxt2 = Ts.ReadLine
'-> If Not There Add It
If InStr(StrTxt1, StrTxt2) = 0 Then
StrTxt3 = StrTxt3 & StrTxt2 & vbCrLf
End If
Loop
Ts.Close
'-> Third Text File
Set Ts = Fso.CreateTextFile("PATH_TO_FILE\Differences.txt")
Ts.WriteLine StrTxt3
Ts.Close

all i did was was change the StrTxt variables around and make the whole thing If = 0, i barely know what im talking about here so dont laugh :D

Edited by bauxite
Link to comment
Share on other sites

sorry again, i spoke too soon, further testing shows it infact only works for some text file pairs as the one i posted above (which i was using to test the scripts)

I was unable to complete the script by myself, no idea why it doesnt work for any two text files..(i think its because of how InStr works when used in that script, it doesnt seem to care for "lines")

but i was thinking of a different approach, something like comparing each line from "2.txt" to every single line one at a time in "1.txt", it could be a better way? although maybe slower.

After more experimentation i figured the line by line approach could be done through batch script, im no pro but it resulted in this amazingly bad (but working) batch file, or so i think, anyway i created two special small text files to test this, as it takes a long time on large files, but works quick for a few lines..

here is the bat code, feel free to laugh this time. :)

setlocal enabledelayedexpansion

set var=1

for /f "delims=" %%S in (2.txt) do ((for /f "delims=" %%G in (1.txt) do (if !var!==1 (If "%%S"=="%%G" (set var=0) ELSE (If "%%G"=="END" echo %%S>>diff.txt)))) && set var=1)

It's a bit of a hack since the string "END" must be at the end of each text file for the batch to know its the "END" lol

translation to VBscript or Jscript help would be appreciated (if its possible in Jscript, anything thats faster lol i dont mind)

files attached in zip archive include, the bat file, the vbs script, both text files and the expected results to compare.

SuperBat.zip

Edited by bauxite
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...