Reading from a file into a variable

Reading from a file into a variable

671
Advanced UserAdvanced User
671

    Aug 10, 2017#1

    Hi there,

    I want to read values into variables. A sample of the file format is at bottom. The only scripting command that I can find to read from the file is:

    Code: Select all

    var char = UltraEdit.activeDocument.currentChar;
    Is this what I should use?

    Many thanks for any reply.

    Code: Select all

    00:28:24,2015-11-06 23:53:11 +0000,Conclusion,,Green,MT42 Beanstalk,MTX
    00:25:47,2015-11-06 23:49:56 +0000,Stacked,,Green,MT42 Beanstalk,MTX

    6,603548
    Grand MasterGrand Master
    6,603548

      Aug 11, 2017#2

      Better is to make a selection and use var sString = UltraEdit.activeDocument.selection to assign selected text to variable sString or work directly with the string variable UltraEdit.activeDocument.selection.

      On small files up to 20 MiB it is also possible to use something like:

      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();
      
         // Select everything in active file.
         UltraEdit.activeDocument.selectAll();
         if (UltraEdit.activeDocument.isSel())  // Is the file not empty?
         {
            // Determine line termination type used in active file.
            var sLineTerm;
            if (UltraEdit.activeDocument.lineTerminator < 0) sLineTerm = "\r\n";
            else if (UltraEdit.activeDocument.lineTerminator == 1) sLineTerm = "\n";
            else sLineTerm = "\r";
      
            // Load entire file content as array of strings into memory with
            // each string being a line from file without line termination.
            var asLines = UltraEdit.activeDocument.selection.split(sLineTerm);
      
            // Do something with the lines in the array of strings.
      
            // Join the lines back to a single string (block) and
            // overwrite entire file content with the modified lines.
            UltraEdit.activeDocument.write(asLines.join(sLineTerm));
         }
      }
      Best regards from an UC/UE/UES for Windows user from Austria

      671
      Advanced UserAdvanced User
      671

        Aug 11, 2017#3

        Thank you Mofi.