Insert Multiple Lines at Beginning in Multiple Files Within a Directory Tree

Insert Multiple Lines at Beginning in Multiple Files Within a Directory Tree

1
NewbieNewbie
1

    Sep 27, 2012#1

    I'm trying to determine if UltraEdit(18.20.0.1017) can help me insert a finite number of lines at the beginning of every file within a directory tree.

    I need to insert Copyright information into C# code that exists within files within a directory tree.

    For Example:

    Original File:
    // Could be Using Statement
    // Could be Namespace Statement
    // Could be Comment

    Output File:
    //**************************************
    // Copyright information
    //**************************************

    // Could be Using Statement
    // Could be Namespace Statement
    // Could be Comment

    6,603548
    Grand MasterGrand Master
    6,603548

      Sep 29, 2012#2

      If that can be done using command Replace in Files depends on size of the files.

      Using Perl Regular Expression search string ((?:.*\r\n)+)(?!.) finds everything in a file. Using as replace string

      //**************************************\r\n// Copyright information\r\n//**************************************\r\n\r\n\1

      results in inserting the copyright string at top of every file.

      But this works only 100% correct if the files are small, i.e. have just some kilobytes as the Perl regular expression engine cannot match megabytes. I can't tell you the absolute file size limit, but on a file with 677 KB this replace worked. But it failed on a file with 15.63 MB.

      17
      Basic UserBasic User
      17

        Dec 22, 2012#3

        I've tested with the Perl Regex:

        "((?:.*\r\n)+)(?!.)"

        But it did not work.

        6,603548
        Grand MasterGrand Master
        6,603548

          Dec 22, 2012#4

          min2max, you have not written with which parameters you executed the Replace in Files and on which files. Therefore your post is useless for everybody else and nobody can really help you.

          I executed with UltraEdit v18.20.0.1028 a Replace in Files with following parameters:

          Find What: ((?:.*\r\n)+)(?!.)
          Replace With: This is just a test\r\nfor Replace in Files.\r\n\1
          Replace Where: Files Listed
          In Files/Types: *.tmp
          Directory: C:\Temp\

          Match Whole Word Only: not checked
          Match Case: checked
          Results to Edit Window: not checked
          Regular Expressions: Perl: checked

          List Changed Files: not checked
          Preserve Case: not available
          Use Encoding: not checked

          Subdirectories to ignore in search: none
          File names/extensions to ignore in search: none

          Search Subdirectories: checked
          Ignore hidden Subdirectories: not checked
          Open matching files: not checked

          Before I executed this command I copied to C:\Temp\ a very small file with changing name to Test1.txt, created a subdirectory named Test and copied into this subdirectory a second, different very small file with changing name to Test2.txt. Both files were modified by the Replace in Files command by inserting at top the 2 lines:

          This is just a test
          for Replace in Files.


          And there were no other occurrences of these 2 lines in the 2 files after Replace in Files.

          Of course both test files contained lines with DOS line terminators. The Replace in Files modifies no file if the files contain UNIX line terminators (only line-feed).

          However, the solution I posted here is in general not good for inserting strings only once at top of multiple files as it is unpredictable how often the string is really inserted into each file. The Replace in Files message box displayed after the replace could indicate easily more replaces than files which means that there were inserted the lines in some files more than once.

          A better approach for this task would be to code a script using function GetListOfFiles to get the full names of all files to modify in a directory tree and function GetFileIndex to verify if a file to update is opened already in UE/UES.

          Code: Select all

          // Insert here function GetListOfFiles.
          
          // Insert here function GetFileIndex.
          
          // See file GetListOfFiles.js for an explanation of the following code up to next comment.
          var bSubFolders=false;
          var bNewOpened=false;
          
          UltraEdit.outputWindow.showStatus=false;
          UltraEdit.outputWindow.clear();
          if (UltraEdit.outputWindow.visible == false) UltraEdit.outputWindow.showWindow(true);
          
          if (UltraEdit.document.length < 1) {
             UltraEdit.newFile();
             bNewOpened = true;
          }
          
          sFolder = UltraEdit.getString("Please enter the path to the directory:",1);
          if ((sFolder == "") || (sFolder == ".\\")) {
             UltraEdit.outputWindow.write("Search directory:  .\\ (= working directory)");
          } else {
             UltraEdit.outputWindow.write("Search directory:  " + sFolder);
          }
          
          sFiles = UltraEdit.getString("Please enter the files/types to find:",1);
          if (sFiles == "") sFiles = "*";
          UltraEdit.outputWindow.write("Find files/types:  " + sFiles);
          
          var nSubFolders = UltraEdit.getValue("Search in subdirectories (0/1 = no/yes)?",1);
          if (nSubFolders) bSubFolders = true;
          var sUsersChoice = bSubFolders ? "true" : "false";
          UltraEdit.outputWindow.write("Search subfolders: " + sUsersChoice);
          
          var sTextToInsert = UltraEdit.getString("Text to insert at top of all files (with \\r\\n):",1);
          
          if (bNewOpened) UltraEdit.closeFile(UltraEdit.document[0].path,2);
          
          
          // Now run function GetListOfFiles to get the file names, except
          // the user as entered nothing to insert at top of every found file.
          
          if (sTextToInsert.length) {
             
             sTextToInsert = sTextToInsert.replace(/\\r/g,"\r");
             sTextToInsert = sTextToInsert.replace(/\\n/g,"\n");
          
             if (GetListOfFiles(0,sFolder,sFiles,bSubFolders)) {
          
                // Get the file names into an array of strings and remove the
                // last item from the array which is always an empty string
                // as the list file ends with a DOS line terminator.
                UltraEdit.activeDocument.selectAll();
                var asFileNames = UltraEdit.activeDocument.selection.split("\r\n");
                var nListFileIndex = GetFileIndex();
                asFileNames.pop();
          
                // Open in a loop the files, insert the text at top of the opened files
                // and close the files with saving. For files opened already in UE/UES
                // just insert the text at top, but do not save and close the file.
                UltraEdit.insertMode();
                if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
                else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
                for (var nFileIndex = 0; nFileIndex < asFileNames.length; nFileIndex++) {
                   var nActiveDocIndex = GetFileIndex(asFileNames[nFileIndex]);
                   if (nActiveDocIndex < 0) {
                      UltraEdit.open(asFileNames[nFileIndex]);
                      UltraEdit.activeDocument.write(sTextToInsert);
                      UltraEdit.closeFile(UltraEdit.activeDocument.path,1);
                   }
                   else {
                      UltraEdit.document[nActiveDocIndex].top();
                      UltraEdit.document[nActiveDocIndex].write(sTextToInsert);
                   }
                }
                UltraEdit.outputWindow.write(nFileIndex.toString() + " file" + (nFileIndex != 1 ? "s" : "") + " updated.");
                // Close now the document window with the file names without saving.
                UltraEdit.closeFile(UltraEdit.document[nListFileIndex].path,2);
             }
          }
          This script can be modified easily to insert at top of all files the text from active clipboard instead of an entered string.

          Further it is also very easy to change the script to replace first occurrence of a string in all files of a directory (tree) by another string.

          And this script can be modified also easily to insert the name of the file anywhere inside the file.

          So this script can be used as template for many operations which should be done on all files in a directory (tree).