"Batch" function to apply a macro to multiple text files

Help with writing and playing macros

"Batch" function to apply a macro to multiple text files

Postby DiamondEagle » Wed Apr 25, 2007 12:11 pm

I've written a macro that will apply some formatting rules to text files. I now need to apply the macro to a few hundred text files in various directories on a server. I need to iterate through all the folders on the server and apply the macro to each of the text files in each of the folders, eg:

\\serverX\2007\Jan (contains 100 text files)
\\serverX\2007\Feb (contains 200 text files)
\\serverX\2007\Mar (contains 500 text files)
\\serverX\2007\Apr (contains 200 text files)

So, there are a total of 1000 text files in the folders in the \\serverX\2007 directory. I need to run the macro on all 1000 text files.

Is there a "batch" command in UE that will allow me to go through each of the folders and apply the macro to all the text files in these folders?
User avatar
DiamondEagle
Newbie
 
Posts: 6
Joined: Fri Apr 06, 2007 11:00 pm

Re: "Batch" function to apply a macro to multiple text files

Postby jorrasdk » Wed Apr 25, 2007 3:31 pm

Hi DiamondEagle

If you searched forum for "batch" you will see several discussions of invoking UE in command line mode and specifying a file list to open and macro to invoke - see for instance:
Search & replace macro from a list of file names

But I have used another approach several times - but it is only possible for UE13 !

Under "Advanced / tool configuration" I have made a tool called "DIR" with performs a simple DIR dos command :
Code: Select all
DIR /A-D-H-S /B /S C:\temp\work\*.xml %modify%

(see DIR /? for explanation)
Tool options: Create new file, capture output, %modify% = popup box with command line

The macro I want to use on multiple files is assigned in "Macro" "Set macro for file load/save". I use "..on load" 1 times.

You might want to guard against this macro being invoked on unwanted files - for example use a test with IfExtIs (tests extension)

Code: Select all
InsertMode
ColumnModeOff
HexOff
UnixReOff
IfExtIs "xml"
"DO STUFF"
EndIf

To process multiple files with the macro of choice, I then use this script:

Code: Select all
//batchInvokeMacro.js
//Configure a tool called "DIR" that performs a DOS DIR command (see in findDirFiles)

var CRLF = "\r\n";

dirFiles = new Array();
findDirFiles();

if (dirFiles.length>0)
  performBatchProcessing();

///////////////////////////////////////////////////////////////////////////////////////////////////
// Call tool "DIR" which make
// dir /A-D-H-S /B /S root-filepath/filemask
// (no dirs, no hidden, no system, short list, subdirectories)
// root-filepath/filemask of your choice (eg. c:\temp\*.txt)
// tool should capture DOS output to new file
function findDirFiles() {
   UltraEdit.runTool("DIR");
   dirDoc = UltraEdit.document[getActiveDocumentIndex()];
   dirDoc.selectAll();
   var dirOutput = dirDoc.selection;
   dirFiles = dirOutput.split(CRLF);
  UltraEdit.closeFile(dirDoc.path,2);
}

/* loop through files and do processing of your choice */
function performBatchProcessing() {
   for (i=0;i<dirFiles.length;i++) {
      if (dirFiles[i].length>0) {
         UltraEdit.open(dirFiles[i]); /* Open file, load macro "fires" */
         UltraEdit.closeFile(dirFiles[i],1); /* save and close */
      }
   }
}

/* Find the tab index of the active document */
function getActiveDocumentIndex() {
   var tabindex = -1; /* start value */

   for (i = 0; i < UltraEdit.document.length; i++)
   {
      if (UltraEdit.activeDocument.path==UltraEdit.document[i].path) {
         tabindex = i;
         break;
      }
   }
   return tabindex;
}

Remember to remove your "..on load" macro option if this was a one time job.

Now adjust the above to your situation and then go get a cut of coffee while the script works its way through a 1000 files. I hope you found some inspiration in this.

Cheers, jorrasdk
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark

Re: "Batch" function to apply a macro to multiple text files

Postby maryv » Thu Apr 26, 2007 5:30 pm

Very slick! Nice work, jorrasdk!
User avatar
maryv
Basic User
Basic User
 
Posts: 26
Joined: Wed Mar 21, 2007 11:00 pm


Return to Macros