bauxite Posted February 3, 2010 Posted February 3, 2010 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.
uid0 Posted February 3, 2010 Posted February 3, 2010 You could try a grep port:http://unxutils.sourceforge.net/http://gnuwin32.sourceforge.net/packages/grep.htmor just fc if you're comparing whole files, or winmerge to do it with a gui.
bauxite Posted February 3, 2010 Author Posted February 3, 2010 ah thanks, so i guess its not possilbe through the default command line tools then?shame, i would prefer to make the batch without using 3rd party tools.
CoffeeFiend Posted February 4, 2010 Posted February 4, 2010 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.
bauxite Posted February 4, 2010 Author Posted February 4, 2010 (edited) 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 February 4, 2010 by bauxite
gunsmokingman Posted February 4, 2010 Posted February 4, 2010 If you posted what you needed to compare I think I could help you write a VBS Script for you.
Yzöwl Posted February 4, 2010 Posted February 4, 2010 If you are comparing the content of files on a line by line basis then, as already indicated, there's a built in commandline tool for that, fc.FC /?
bauxite Posted February 5, 2010 Author Posted February 5, 2010 (edited) 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:abcdefghds8745fjhjgsjnn4C:\im random\ini.txtand 2.txt is almost similar:Xbcdefghds8745fjhjgsjnn4C:\im random\ini.txtim new.txtso 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.txtwhich 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 February 5, 2010 by bauxite
gunsmokingman Posted February 5, 2010 Posted February 5, 2010 I have not tested this, but this VBS script might do what you want.Fill in the pathsPATH_TO_FILE\SomeFile1.txtPATH_TO_FILE\SomeFile2.txtPATH_TO_FILE\Differences.txtSave 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
bauxite Posted February 5, 2010 Author Posted February 5, 2010 on running i get windows script host error,line:1Char:10Error:Type mismatch:'Createect'Code:800A00DSource:Microsoft VBscript runtime error
gunsmokingman Posted February 5, 2010 Posted February 5, 2010 Sorry my mistake on the spelling it should be Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")Not this Dim Fso :Set Fso = Createect("Scripting.FileSystemect")
CoffeeFiend Posted February 5, 2010 Posted February 5, 2010 (edited) 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 February 5, 2010 by CoffeeFiend
bauxite Posted February 5, 2010 Author Posted February 5, 2010 (edited) 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.Closeall 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 Edited February 5, 2010 by bauxite
bauxite Posted February 5, 2010 Author Posted February 5, 2010 (edited) 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 enabledelayedexpansionset var=1for /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" loltranslation 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 February 6, 2010 by bauxite
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now