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

Help with writing and running scripts

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

Postby check1 » Sun Feb 07, 2010 1:25 pm

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?
check1
Newbie
 
Posts: 1
Joined: Sun Feb 07, 2010 1:20 pm

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

Postby Mofi » Mon Feb 08, 2010 5:21 am

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.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3936
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

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

Postby Tim » Wed Mar 21, 2012 12:52 pm

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"
Tim
Basic User
Basic User
 
Posts: 14
Joined: Tue Aug 19, 2008 1:06 pm
Location: Ottawa, Ontario

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

Postby Mofi » Thu Mar 22, 2012 1:19 am

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.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3936
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

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

Postby Tim » Thu Mar 22, 2012 9:59 am

Thanks very much again Mofi. The script did exactly what it was supposed to do once I got the right code in there (function GetNameOfFile, instead of GetFileName).

Cheers!
Tim
Tim
Basic User
Basic User
 
Posts: 14
Joined: Tue Aug 19, 2008 1:06 pm
Location: Ottawa, Ontario


Return to Scripts