User to user discussion and support for UltraEdit, UEStudio, UltraCompare, and other IDM applications.

Help with writing and running scripts
6 posts Page 1 of 1
Hi

I need to take a 2 column list of files and cycle through them and have a file comparison between the two. The output would need to be File A line# = "string of changes" and under it File B line# = "string of changes". For each file.

Sort of like the results you get in UltraCompare but because I have over 4K files it's not feasible to use this program.

My input looks like this with a single horizontal tab between the two lists:

Code: Select all
DMC-GAASIB0-01-82-00-00-0000B-042A-A   DMC-GAASIB0-02-82-00-00-00AAA-042C-A
DMC-GAASIB0-01-61-00-00-0000A-018A-A   DMC-GAASIB0-00-61-00-00-00AAA-044E-A
DMC-GAASIB0-01-61-20-00-0000A-042A-A   DMC-GAASIB0-00-61-21-00-00AAA-042A-A
DMC-GAASIB0-01-57-00-00-00000-042A-B   DMC-GAASIB0-00-57-00-00-00AAA-044A-A
DMC-GAASIB0-01-57-01-00-00000-042A-B   DMC-GAASIB0-00-57-00-00-01AAA-042A-A
DMC-GAASIB0-01-57-03-00-00000-042A-B   DMC-GAASIB0-00-57-00-00-02AAA-042A-A

Your help is greatly appreciated in this.
Thank you.
What exactly should be output for the input example? And where should the output written to?

With the Perl regular expression search string ^([^\t]+)\t\1$ it is possible to find lines where string left of tab is equal string right of tab if option Match case is also checked to mark them or remove them to keep just lines with different strings.
Best regards from Austria
It's the content of each file that needs to be opened and compared.

So first is the file names to open:
Code: Select all
DMC-GAASIB0-01-82-00-00-0000B-042A-A.txt   DMC-GAASIB0-02-82-00-00-00AAA-042C-A.txt


The files are already formatted to have a sentence on each line. Text comparison is each line of the two files against each other.
Code: Select all
File A -DMC-GAASIB0-01-82-00-00-0000B-042A-A.txt Input:

General Overview
The SPAM has an oven, stove, and sink.
It has this years golden refrigerator.
Other things you should know:
There are marble floors.
The stoves are restaurant quality.


File B -DMC-GAASIB0-02-82-00-00-00AAA-042C-A.txt Input:

General Summary
The SPAM has an oven, stove, dishwasher and sink.
It has this years golden refrigerator.

Other things you should know:
There are stone tiled floors.
The stoves are restaurant quality.


The output is in a new file saved in C:\temp\DMC-GAASIB0-01-82-00-00-0000B-042A-A_COMPARED.txt

Code: Select all

Differences:

File A line 1 = General Overview
File B line 1 = General Summary

File A line 2 = The SPAM has an oven, stove, and sink.
File B line 3 = The SPAM has an oven, stove, dishwasher and sink.

File A line 5 = There are marble floors.
File B line 6 = There are stone tiled floors.


I know this can be done in UltraCompare but there are 4K files to do and opening them up individually and comparing them isn't feasible.
If there was some way to open both files in UltraCompare and save the output to PDF this would be ideal. But I can't find anything that will produce those results.

Thank you very much for your help,
Max
I think I've come up with a solution.

I've created a script to take each file tab file and write it to the UltraCompare command line string. Then I'm hoping to put it all in a batch file. When run UltraCompare will put the comparison into a file and append each pairs results in that file. I then wrote a script to remove all the matching lines from the results file, and give me one large file listing each file and the comparison finds.

My script to create the UltraCompare command line.

Code: Select all
UltraEdit.insertMode();
UltraEdit.columnModeOff();
UltraEdit.perlReOn();
UltraEdit.activeDocument.findReplace.mode=0;
UltraEdit.activeDocument.findReplace.matchCase=false;
UltraEdit.activeDocument.findReplace.matchWord=false;
UltraEdit.activeDocument.findReplace.regExp=true;
UltraEdit.activeDocument.findReplace.searchDown=true;
UltraEdit.activeDocument.findReplace.searchInColumn=false;
UltraEdit.activeDocument.findReplace.preserveCase=false;
UltraEdit.activeDocument.findReplace.replaceAll=true;
UltraEdit.activeDocument.top();

while (UltraEdit.activeDocument.findReplace.find("DMC.*?\tDMC.*?$")){
   UltraEdit.activeDocument.key("HOME");
   if (UltraEdit.activeDocument.findReplace.find("DMC.*?\tDMC.*?$"))
   {
      UltraEdit.activeDocument.findReplace.replace(" ", "");
      var sFile1 = UltraEdit.activeDocument.selection.replace(/(DMC.*?)\t(DMC.*?)$/,"$1");
      var sFile2 = UltraEdit.activeDocument.selection.replace(/(DMC.*?)\t(DMC.*?)$/,"$2");
      var sFile1New = "C:\\test\\Compare Files for Max\\test output\\" + sFile1 + ".txt";
      var sFile2New = "C:\\test\\Compare Files for Max\\test output\\" + sFile2 + ".txt";
      var sUC = "uc -i -lt -B -b -t \" ";
      var sPath = sFile1New + "\" \"" + sFile2New + "\"";
      var sOutputFile = "-op " + "\"c:\ temp\ComparisonResults.txt\"";
      var sFullUC = sUC + sPath + sOutputFile;
     
      UltraEdit.outputWindow.write(sFullUC);
   }
}

UE script to clean up the compared results file.

Code: Select all
UltraEdit.perlReOn();
// Define environment for this script.
UltraEdit.insertMode();
UltraEdit.columnModeOff();

// Set new search parameters
UltraEdit.activeDocument.findReplace.mode=0;
// Set search matchCase off
UltraEdit.activeDocument.findReplace.matchCase=false;
// Set search matchWord off
UltraEdit.activeDocument.findReplace.matchWord=false;
// Set search regular expressions on
UltraEdit.activeDocument.findReplace.regExp=true;
// Set search down on
UltraEdit.activeDocument.findReplace.searchDown=true;
// Set search in column off
UltraEdit.activeDocument.findReplace.searchInColumn=false;

UltraEdit.activeDocument.top();
while (UltraEdit.activeDocument.findReplace.find("^[0-9]+\t  \t.*$")){
   UltraEdit.activeDocument.findReplace.replace("^[0-9]+\t  \t.*$","");
}

UltraEdit.activeDocument.top();
while (UltraEdit.activeDocument.findReplace.find("^$"))
{
   UltraEdit.activeDocument.deleteLine();
}
Before I read your last post I wrote first this simple batch file for comparing the files stored in the list file with UltraCompare.

Code: Select all
@echo off
setlocal EnableDelayedExpansion
set "Directory=C:\Temp"
set "Extension=.txt"

rem Very simple and quick check if UltraCompare program files directory
rem is in PATH and if this is not the case, add it to local copy of PATH
rem at beginning as no other Windows standard console applications from
rem directory %SystemRoot%\System32 are required for this batch script.
if "%PATH:UltraCompare=%" == "%PATH%" set "PATH=%ProgramFiles%\IDM Computer Solutions\UltraCompare;%PATH%"

for /F "usebackq tokens=1,2" %%I in ("%Directory%\ListFile.txt") do (
    for /F %%# in ('uc.com -qc "%Directory%\%%I%Extension%" "%Directory%\%%J%Extension%"') do set "Result=%%#"
    if "!Result:~0,4!" == "Diff" uc.com -B -ne -t "%Directory%\%%I%Extension%" "%Directory%\%%J%Extension%" -o "%Directory%\%%I_Compared_UC%Extension%"
)
endlocal

Note: The string values assigned to the environment variables Directory and Extension might be modified as well as path to program files folder of UltraCompare and path and name of the list file ListFile.txt.

And I wrote also an UltraEdit scripting solution for this file comparison task only poorly tested.

Code: Select all
if (UltraEdit.document.length > 0)  // Is any file opened?
{
   var sLineTerm  = "\r\n";
   var sDirectory = "C:\\Temp\\";
   var sExtension = ".txt";

   // Define environment for this script.
   UltraEdit.insertMode();
   UltraEdit.columnModeOff();

   // Active file should be the least file with the file names.
   UltraEdit.activeDocument.selectAll();
   if (UltraEdit.activeDocument.isSel())
   {
      // Load the file name pairs into an array of strings.
      var asFileNames = UltraEdit.activeDocument.selection.split(sLineTerm);
      // Remove the last string if it is an empty string because
      // the list file ends with a DOS/Windows line termination.
      if (!asFileNames[asFileNames.length-1].length) asFileNames.pop();
      // Cancel the selection in list file kept open although not further used.
      UltraEdit.activeDocument.top();

      // Run the next loop for each file name pair.
      for (nFileNameIndex = 0; nFileNameIndex < asFileNames.length; nFileNameIndex++)
      {
         // Get position of tab character seperating the two file names.
         var nTabPos = asFileNames[nFileNameIndex].indexOf("\t");
         if (nTabPos < 0) continue;    // Skip this line if there is no tab.

         // Get the two file names.
         var sFileNameLeft = asFileNames[nFileNameIndex].substr(0,nTabPos) + sExtension;
         var sFileNameRight = asFileNames[nFileNameIndex].substr(nTabPos+1) + sExtension;

         // Load all lines from left file.
         UltraEdit.open(sDirectory+sFileNameLeft);
         UltraEdit.activeDocument.selectAll();
         var asLinesLeft = UltraEdit.activeDocument.selection.split(sLineTerm);
         UltraEdit.closeFile(UltraEdit.activeDocument.path,2);

         // Load all lines from right file.
         UltraEdit.open(sDirectory+sFileNameRight);
         UltraEdit.activeDocument.selectAll();
         var asLinesRight = UltraEdit.activeDocument.selection.split(sLineTerm);
         UltraEdit.closeFile(UltraEdit.activeDocument.path,2);

         // Remove from lines of right file all non empty lines also in left file.
         var nLineIndexLeft;
         var nLineIndexRight;
         for (nLineIndexLeft = 0; nLineIndexLeft < asLinesLeft.length; nLineIndexLeft++)
         {
            if (!asLinesLeft[nLineIndexLeft].length) continue;
            for (nLineIndexRight = 0; nLineIndexRight < asLinesRight.length; nLineIndexRight++)
            {
               if (asLinesRight[nLineIndexRight] == asLinesLeft[nLineIndexLeft])
               {
                  asLinesRight[nLineIndexRight] = "#";
                  asLinesLeft[nLineIndexLeft] = "#";
                  break;
               }
            }
         }

         nLineIndexLeft = 0;
         nLineIndexRight = 0;
         var sLineLeft = "";
         var sLineRight = "";
         var sDifferences = "";
         var bNewDiffBlock = false;

         while ((nLineIndexLeft < asLinesLeft.length) || (nLineIndexRight < asLinesRight.length))
         {
            while (nLineIndexLeft < asLinesLeft.length)
            {
               if (!asLinesLeft[nLineIndexLeft].length)
               {
                  nLineIndexLeft++;
               }
               else
               {
                  if (asLinesLeft[nLineIndexLeft] != "#")
                  {
                     sLineLeft = "File L " + "line " + (nLineIndexLeft+1).toString(10) +
                                 " = " + asLinesLeft[nLineIndexLeft] + sLineTerm;
                  }
                  break;
               }
            }

            while (nLineIndexRight < asLinesRight.length)
            {
               if (!asLinesRight[nLineIndexRight].length)
               {
                  nLineIndexRight++;
               }
               else
               {
                  if (asLinesRight[nLineIndexRight] != "#")
                  {
                     sLineRight = "File R " + "line " + (nLineIndexRight+1).toString(10) +
                                  " = " + asLinesRight[nLineIndexRight] + sLineTerm;
                  }
                  break;
               }
            }

            // Are there in both files different lines?
            if (sLineLeft.length && sLineRight.length)
            {
               sDifferences += sLineTerm + "*  " + sLineLeft + "*  " + sLineRight;
               sLineLeft = "";
               sLineRight = "";
               nLineIndexLeft++;
               nLineIndexRight++;
               bNewDiffBlock = true;
            }

            // Are there in both files identical lines?
            else if (!sLineLeft.length && !sLineRight.length)
            {
               nLineIndexLeft++;
               nLineIndexRight++;
               bNewDiffBlock = true;
            }
            else  // There are only in one file lines not existing in other file.
            {
               if (bNewDiffBlock)
               {
                  bNewDiffBlock = false;
                  sDifferences += sLineTerm;
               }
               if (sLineLeft.length)   // Does the line exist only in left file?
               {
                  sDifferences += "<! " + sLineLeft;
                  sLineLeft = "";
                  nLineIndexLeft++;
               }
               else                    // The line exists only in right file?
               {
                  sDifferences += "!> " + sLineRight;
                  sLineRight = "";
                  nLineIndexRight++;
               }
            }
         }

         if (sDifferences.length)
         {
            // A new file is created for the difference output.
            UltraEdit.newFile();
            UltraEdit.activeDocument.unixMacToDos();
            UltraEdit.activeDocument.write("File L: " + sDirectory + sFileNameLeft + sLineTerm);
            UltraEdit.activeDocument.write("File R: " + sDirectory + sFileNameRight + sLineTerm);
            UltraEdit.activeDocument.write(sLineTerm + "Differences:" + sLineTerm);
            UltraEdit.activeDocument.write(sDifferences);
            UltraEdit.saveAs(sDirectory+sFileNameLeft.replace(sExtension,"_Compared_UE"+sExtension));
            UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
            sDifferences = "";
         }
      }
   }
}

Note: The string values assigned to the JavaScript string variables sDirectory and sExtension might be modified. The list file must be the active file on script execution.

The batch script as well as the UltraEdit script expect both a list file containing per line two file names without file extension separated by a single tab character and not containing 1 or more spaces in any file name.

I did not know that you now wanted all different results in a single file instead of individual files with name depending on left file name. That would be of course also very easy to achieve by using in batch file -op instead of -o with always same file name like "%Directory%\DiffResults%Extension%" and by writing all differences by the UltraEdit script into same new file created once at beginning instead of a new file for each file pair as it can be seen on script code below.

Code: Select all
if (UltraEdit.document.length > 0)  // Is any file opened?
{
   var sLineTerm  = "\r\n";
   var sDirectory = "C:\\Temp\\";
   var sExtension = ".txt";

   // Define environment for this script.
   UltraEdit.insertMode();
   UltraEdit.columnModeOff();

   // Active file should be the least file with the file names.
   UltraEdit.activeDocument.selectAll();
   if (UltraEdit.activeDocument.isSel())
   {
      // Load the file name pairs into an array of strings.
      var asFileNames = UltraEdit.activeDocument.selection.split(sLineTerm);
      // Remove the last string if it is an empty string because
      // the list file ends with a DOS/Windows line termination.
      if (!asFileNames[asFileNames.length-1].length) asFileNames.pop();
      // Cancel the selection in list file kept open although not further used.
      UltraEdit.activeDocument.top();

      var nResultsFileIndex = UltraEdit.document.length
      UltraEdit.newFile();
      var oResultsFile = UltraEdit.document[nResultsFileIndex];
      UltraEdit.activeDocument.unixMacToDos();

      // Run the next loop for each file name pair.
      for (nFileNameIndex = 0; nFileNameIndex < asFileNames.length; nFileNameIndex++)
      {
         // Get position of tab character seperating the two file names.
         var nTabPos = asFileNames[nFileNameIndex].indexOf("\t");
         if (nTabPos < 0) continue;    // Skip this line if there is no tab.

         // Get the two file names.
         var sFileNameLeft = asFileNames[nFileNameIndex].substr(0,nTabPos) + sExtension;
         var sFileNameRight = asFileNames[nFileNameIndex].substr(nTabPos+1) + sExtension;

         // Load all lines from left file.
         UltraEdit.open(sDirectory+sFileNameLeft);
         UltraEdit.activeDocument.selectAll();
         var asLinesLeft = UltraEdit.activeDocument.selection.split(sLineTerm);
         UltraEdit.closeFile(UltraEdit.activeDocument.path,2);

         // Load all lines from right file.
         UltraEdit.open(sDirectory+sFileNameRight);
         UltraEdit.activeDocument.selectAll();
         var asLinesRight = UltraEdit.activeDocument.selection.split(sLineTerm);
         UltraEdit.closeFile(UltraEdit.activeDocument.path,2);

         // Remove from lines of right file all non empty lines also in left file.
         var nLineIndexLeft;
         var nLineIndexRight;
         for (nLineIndexLeft = 0; nLineIndexLeft < asLinesLeft.length; nLineIndexLeft++)
         {
            if (!asLinesLeft[nLineIndexLeft].length) continue;
            for (nLineIndexRight = 0; nLineIndexRight < asLinesRight.length; nLineIndexRight++)
            {
               if (asLinesRight[nLineIndexRight] == asLinesLeft[nLineIndexLeft])
               {
                  asLinesRight[nLineIndexRight] = "#";
                  asLinesLeft[nLineIndexLeft] = "#";
                  break;
               }
            }
         }

         nLineIndexLeft = 0;
         nLineIndexRight = 0;
         var sLineLeft = "";
         var sLineRight = "";
         var sDifferences = "";
         var bNewDiffBlock = false;

         while ((nLineIndexLeft < asLinesLeft.length) || (nLineIndexRight < asLinesRight.length))
         {
            while (nLineIndexLeft < asLinesLeft.length)
            {
               if (!asLinesLeft[nLineIndexLeft].length)
               {
                  nLineIndexLeft++;
               }
               else
               {
                  if (asLinesLeft[nLineIndexLeft] != "#")
                  {
                     sLineLeft = "File L " + "line " + (nLineIndexLeft+1).toString(10) +
                                 " = " + asLinesLeft[nLineIndexLeft] + sLineTerm;
                  }
                  break;
               }
            }

            while (nLineIndexRight < asLinesRight.length)
            {
               if (!asLinesRight[nLineIndexRight].length)
               {
                  nLineIndexRight++;
               }
               else
               {
                  if (asLinesRight[nLineIndexRight] != "#")
                  {
                     sLineRight = "File R " + "line " + (nLineIndexRight+1).toString(10) +
                                  " = " + asLinesRight[nLineIndexRight] + sLineTerm;
                  }
                  break;
               }
            }

            // Are there in both files different lines?
            if (sLineLeft.length && sLineRight.length)
            {
               sDifferences += sLineTerm + "*  " + sLineLeft + "*  " + sLineRight;
               sLineLeft = "";
               sLineRight = "";
               nLineIndexLeft++;
               nLineIndexRight++;
               bNewDiffBlock = true;
            }

            // Are there in both files identical lines?
            else if (!sLineLeft.length && !sLineRight.length)
            {
               nLineIndexLeft++;
               nLineIndexRight++;
               bNewDiffBlock = true;
            }
            else  // There are only in one file lines not existing in other file.
            {
               if (bNewDiffBlock)
               {
                  bNewDiffBlock = false;
                  sDifferences += sLineTerm;
               }
               if (sLineLeft.length)   // Does the line exist only in left file?
               {
                  sDifferences += "<! " + sLineLeft;
                  sLineLeft = "";
                  nLineIndexLeft++;
               }
               else                    // The line exists only in right file?
               {
                  sDifferences += "!> " + sLineRight;
                  sLineRight = "";
                  nLineIndexRight++;
               }
            }
         }

         if (sDifferences.length)
         {
            if (oResultsFile.currentLineNum > 1)
            {
               oResultsFile.write(sLineTerm + "------------------------------------------------------------" + sLineTerm +sLineTerm);
            }
            oResultsFile.write("File L: " + sDirectory + sFileNameLeft + sLineTerm);
            oResultsFile.write("File R: " + sDirectory + sFileNameRight + sLineTerm);
            oResultsFile.write(sLineTerm + "Differences:" + sLineTerm);
            oResultsFile.write(sDifferences);
            sDifferences = "";
         }
      }
   }
}
Best regards from Austria
Thank you Mofi, as always your scripts are incredible!
6 posts Page 1 of 1