How to modify identifier in all files of a folder with including an incrementing number?

How to modify identifier in all files of a folder with including an incrementing number?

10
Basic UserBasic User
10

    May 25, 2015#1

    I have any number of files. I want to change a record that look like this:

    <TrackingId>somedata</TrackingId>

    where "somedata" will vary from file to file.

    I would like to change "somedata" to be

    <TrackingId>REC01</TrackingId> for file one
    <TrackingId>REC02</TrackingId> for file two
    <TrackingId>REC03</TrackingId> for file three

    Until I exhaust all the files in directory that I filtered.

    I want REC to be "REC; :?: :?: I want the NN piece to be the ordinal file number as the replace progress so it would like.

    Can this be done?

    So some how a variable that is the file count will have to be part of the replace argument. Can some kind soul share with me how to do that.

    Thanks.

    6,675585
    Grand MasterGrand Master
    6,675585

      May 25, 2015#2

      Find/Replace can just search for a string and replace each found string with another string. Other operations like inserting in replace string a variable which is increased on some criteria is not possible. This requires a scripting solution.

      First, download, copy and paste function GetListOfFiles into a new ASCII file with DOS line terminators. Copy and paste just the function without the comments above and the demo example code below. But read the comments, especially in case of not using English UltraEdit.

      Next copy and paste into the file also following scripting code:

      Code: Select all

      if (GetListOfFiles(0,"C:\\Temp\\","*.xml))
      {
         UltraEdit.activeDocument.selectAll();
         var sLineTerm = "\r\n";   // Default line terminator type is DOS.
         var nLineTermPos = UltraEdit.activeDocument.selection.search(/\r\n|\n|\r/);
         if (nLineTermPos >= 0)    // Any line terminator found?
         {
            // The list file is a Unix file if first character found is a line-feed.
            if (UltraEdit.activeDocument.selection[nLineTermPos] == '\n') sLineTerm = "\n";
            // The list file is a Mac file if first character found is a carriage
            // return and the next character is not a line-feed as in a DOS file.
            else if (UltraEdit.activeDocument.selection[nLineTermPos+1] != '\n') sLineTerm = "\r";
         }
         var asFileNames = UltraEdit.activeDocument.selection.split(sLineTerm);
      
         // The list is not needed anymore and therefore the results window is closed.
         UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
         asFileNames.pop();  // Remove empty string at end of the list.
      
         // Define the parameters for the Replace in Files executed on
         // each file in the list to change the tracking identifier.
         UltraEdit.ueReOn();
         UltraEdit.frInFiles.filesToSearch = 0;
         UltraEdit.frInFiles.directoryStart = "";
         UltraEdit.frInFiles.ignoreHiddenSubs = false;
         UltraEdit.frInFiles.openMatchingFiles = false;
         UltraEdit.frInFiles.unicodeSearch = false;
         UltraEdit.frInFiles.preserveCase = false;
         UltraEdit.frInFiles.searchSubs = false;
         UltraEdit.frInFiles.logChanges = true;
         UltraEdit.frInFiles.matchWord = false;
         UltraEdit.frInFiles.matchCase = true;
         UltraEdit.frInFiles.regExp = true;
      
         // Convert the number of file names to a string and replace each digit
         // by a zero for having finally a string consisting only of zeros with
         // maximum number of digits needed for all files.
         var sLeadingZeros = asFileNames.length.toString(10).replace(/./g,"0");
         var nZeroCount = sLeadingZeros.length;
         var nFileIndex = 0;
      
         // Run on each file in list an UltraEdit regular expression Replace in Files
         // replacing all tracking identifiers in the file by a new one with file
         // index starting with 1 in replace string.
         while (nFileIndex < asFileNames.length)
         {
            UltraEdit.frInFiles.searchInFilesTypes = asFileNames[nFileIndex];
            nFileIndex++;
            var sNumber = nFileIndex.toString(10);
            var sFileNumber = sLeadingZeros.substr(0,nZeroCount - sNumber.length) + sNumber;
            UltraEdit.frInFiles.replace("<TrackingId>*</TrackingId>","<TrackingId>Rec" + sFileNumber + "</TrackingId>");
         }
      }
      
      Edited by you must be the directory path "C:\\Temp\\" (always 2 backslash for each backslash in path) and the file type *.xml.

      Then save the file for example with name ChangeTrackingIdInFiles.js.

      Last execute the script by clicking on menu item Run Active Script in menu Scripting.

      You can see how many replace were made in which files in not automatically opened output window after script execution finished.
      Best regards from an UC/UE/UES for Windows user from Austria