Replace in all files a placeholder string with the directory/name of the file

Replace in all files a placeholder string with the directory/name of the file

1
NewbieNewbie
1

    Feb 07, 2010#1

    I've got a lot of files (>1.000), each file is in a unique directory. What I want to do is replace a string (constant) within each file, with the directory path that the file in question is located in. Is this possible?

    6,675585
    Grand MasterGrand Master
    6,675585

      Feb 08, 2010#2

      Not with a simple find and replace action. You need a script for this task. The most difficult jobs for this script are already done, see

      Function to get list of files into an edit window
      General file name evaluating functions

      Here is the small script which worked on examples I quickly created for testing.

      Code: Select all

      UltraEdit.ueReOn();
      if (GetListOfFiles(0,"C:\\Temp\\","*.tmp",true)) {
         UltraEdit.activeDocument.selectAll();
         var sFileNamesList = UltraEdit.activeDocument.selection;
         var asFileNames = sFileNamesList.split("\r\n");
         var nFileCount = asFileNames.length;
         if (!asFileNames[nFileCount-1].length) nFileCount--;
         UltraEdit.frInFiles.directoryStart="";
         UltraEdit.frInFiles.filesToSearch=0;
         UltraEdit.frInFiles.ignoreHiddenSubs=false;
         UltraEdit.frInFiles.logChanges=true;
         UltraEdit.frInFiles.matchCase=true;
         UltraEdit.frInFiles.matchWord=false;
         UltraEdit.frInFiles.preserveCase=false;
         UltraEdit.frInFiles.searchSubs=false;
         UltraEdit.frInFiles.regExp=false;
         UltraEdit.frInFiles.unicodeSearch=false;
         while(nFileCount--) {
            var sFilePath = GetFilePath(asFileNames[nFileCount]);
            UltraEdit.frInFiles.searchInFilesTypes=asFileNames[nFileCount];
            UltraEdit.frInFiles.replace("%current directory%",sFilePath);
         }
      }
      You have to download and copy function GetListOfFiles into your script file and adapt it according to the description if you are not working with the English version of UltraEdit or when you have modified the find output format settings in the configuration dialog.

      You also have to download the script file with the file name functions and copy function GetFilePath into your script file.

      Last you have to adapt the 3 strings for initial directory "C:\\Temp\\", file types "*.tmp" and search string "%current directory%" according to your environment.

      14
      Basic UserBasic User
      14

        Mar 21, 2012#3

        I want to replace a specific string that occurs in 6000 files (file0001.htm-file6000.htm for this example) with the name of each of those files.

        [\./]*thisdirectory/thisfilename.html is the UltraEdit regular expression search string to find thisdirectory/thisfilename.html appearing in all 6000+ files, but in different depths ../, ../../, etc.

        Every found string in each of the files should be replaced with the name of the file. I don't need any relative paths because all of these files are in the same directory.

        "file0001.htm"
        ....
        "file6000.htm"

        6,675585
        Grand MasterGrand Master
        6,675585

          Mar 22, 2012#4

          See my first post for an explanation of the script below with the small changes needed for your task.

          Code: Select all

          UltraEdit.ueReOn();
          if (GetListOfFiles(0,"C:\\Temp\\","*.htm",true)) {
             UltraEdit.activeDocument.selectAll();
             var sFileNamesList = UltraEdit.activeDocument.selection;
             var asFileNames = sFileNamesList.split("\r\n");
             var nFileCount = asFileNames.length;
             if (!asFileNames[nFileCount-1].length) nFileCount--;
             UltraEdit.frInFiles.directoryStart="";
             UltraEdit.frInFiles.filesToSearch=0;
             UltraEdit.frInFiles.ignoreHiddenSubs=false;
             UltraEdit.frInFiles.logChanges=true;
             UltraEdit.frInFiles.matchCase=true;
             UltraEdit.frInFiles.matchWord=false;
             UltraEdit.frInFiles.preserveCase=false;
             UltraEdit.frInFiles.searchSubs=false;
             UltraEdit.frInFiles.regExp=true;
             UltraEdit.frInFiles.unicodeSearch=false;
             while(nFileCount--) {
                var sFileName = GetNameOfFile(asFileNames[nFileCount]);
                UltraEdit.frInFiles.searchInFilesTypes=asFileNames[nFileCount];
                UltraEdit.frInFiles.replace("\"[\\./]*thisdirectory/thisfilename.html\"","\""+sFileName+"\"");
             }
          }
          In comparison to the script in my first post I have enabled regular expression search using UltraEdit engine. I have replaced GetFilePath by GetNameOfFile, so you have to embed GetNameOfFile into the script file. I have replaced the search string with your expression. And I have renamed the string variable. Finally I have changed *.tmp to *.htm in second line.

          So you have to set only the initial directory correct in first line and insert the 2 functions GetListOfFiles and GetNameOfFile at top of the script file.

          14
          Basic UserBasic User
          14

            Mar 22, 2012#5

            Thanks very much again Mofi. The script did exactly what it was supposed to do.

            Cheers!
            Tim