Count characters not including backslash

Count characters not including backslash

1
NewbieNewbie
1

    Sep 22, 2015#1

    First time post new user.

    I would like a macro/script that counts the number of characters not including \ then divide that by 2 and display the result.

    Seems easy but I'm struggling with it.

    Thanks
    LB

    6,686585
    Grand MasterGrand Master
    6,686585

      Sep 22, 2015#2

      If a string is selected, the number of selected bytes (not characters) are displayed in the status bar at bottom of UE main window.

      It is also possible to use Search - Word Count which contains the number of selected characters.

      But it is of course possible to write a simple UltraEdit script to get the number of characters divided by 2 ignoring all backslashes.

      Code: Select all

      if (UltraEdit.document.length > 0)  // Is any file opened?
      {
         // Define environment for this script.
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
      
         var nTotalChars = 0;
         var bSelectAll = false;
      
         // Select entire file if there is currently nothing selected.
         if (!UltraEdit.activeDocument.isSel())
         {
            var nLineNum = UltraEdit.activeDocument.currentLineNum;
            var nColumnNum = UltraEdit.activeDocument.currentColumnNum;
            if (typeof(UltraEdit.activeDocumentIdx) == "undefined") nColumnNum++;
            UltraEdit.activeDocument.selectAll();
            bSelectAll = true;
         }
         // Process selection only if there is now a selection at all,
         // i.e. the file is not an empty file.
         if (UltraEdit.activeDocument.isSel())
         {
            // Get selected text with all backslashes removed.
            var sCharsNoBackslash = UltraEdit.activeDocument.selection.replace(/\\/g,"");
            nTotalChars = sCharsNoBackslash.length;
            // Change an odd number to next even number.
            if (nTotalChars & 0x01) nTotalChars++;
            // Divide number of characters by 2.
            nTotalChars >>= 1;
         }
         if (bSelectAll)   // Cancel the selection and set caret back to initial
         {                 // position in file if the entire file was selected before.
            UltraEdit.activeDocument.gotoLine(nLineNum,nColumnNum);
         }
         UltraEdit.messageBox("Number of characters is: "+nTotalChars.toString(10));
      }
      Note: This script does not work for files of any size as it loads the selected block or entire file into memory which fails if file is very large.
      Best regards from an UC/UE/UES for Windows user from Austria