EmEditor (text editor) Forum Index
   Questions and Answers about Macros
     Run macro for all files in a folder?
Register To Post

Threaded | Newest First Previous Topic | Next Topic | Bottom
Poster Thread
nickb
Posted on: 3/22/2009 9:42 am
Just popping in
Joined: 3/18/2008
From:
Posts: 20
Run macro for all files in a folder?
Hello,

Is there a way to run a macro (.jsee) on all files in a folder? If you have, for example, 500 files and you want to run a macro for all of them...Is opening all files the only solution to accomplish this?

Thx!
Yutaka
Posted on: 3/22/2009 6:36 pm
Webmaster
Joined: 9/28/2006
From: Redmond
Posts: 2398
Re: Run macro for all files in a folder?
Quote:

nickb wrote:
Hello,

Is there a way to run a macro (.jsee) on all files in a folder? If you have, for example, 500 files and you want to run a macro for all of them...Is opening all files the only solution to accomplish this?

Thx!


You can write a macro to enumerate all files within a folder. It should be easy by using an external object "FileSystemObject". See Files Collection for an example.


----------------
Yutaka Emura
Developer of EmEditor
http://www.emeditor.com/

nickb
Posted on: 3/23/2009 10:14 am
Just popping in
Joined: 3/18/2008
From:
Posts: 20
Re: Run macro for all files in a folder?
Thanks Yutaka

I'll try to work something out.

gr.
arnold
Posted on: 3/27/2009 4:52 pm
Just popping in
Joined: 3/27/2009
From:
Posts: 5
Re: Run macro for all files in a folder?
hi nick. it sounds very interesting. can you post your working script, please? ty ! Arnold
arnold
Posted on: 4/20/2009 2:49 pm
Just popping in
Joined: 3/27/2009
From:
Posts: 5
Re: Run macro for all files in a folder?
no-one with a working script to run macros on a folder?
Yutaka
Posted on: 4/20/2009 3:01 pm
Webmaster
Joined: 9/28/2006
From: Redmond
Posts: 2398
Re: Run macro for all files in a folder?
This is my sample to check if <title> and <h2> matches in all HTML files in a folder (searches the folder recursively). You would of course need to modify the root folder, the contents of DoSomething() function, and the file extension to search.


sRootDir = "E:\\help\\...";   // root folder
editor.NewFile(); 
var docResult = editor.ActiveDocument; 
docResult.writeln( "ERRORS Root Folder: " + sRootDir );

Redraw = false;
var nTotalFiles = EnumerateFolder( sRootDir );
Redraw = true;

alert( "Finished successfully. Total of " + nTotalFiles + " files processed." );


function EnumerateFolder( sFolder )
{
    var nTotal = 0;
    var fso, f, fc, s;
    fso = new ActiveXObject("Scripting.FileSystemObject");
    f = fso.GetFolder(sFolder);
    fc = new Enumerator(f.files);
    for (; !fc.atEnd(); fc.moveNext())
    {
        sItem = String( fc.item() );
        nLen = sItem.length;
        if( sItem.indexOf( "\\_" ) == -1 && sItem.substr( nLen - 4 ) == ".htm" ){  // make sure file or folder does not begin with _ and the file extension is .htm.
            DoSomething( sItem );
            nTotal++;
        }
    }

    fc = new Enumerator(f.SubFolders);
    s = "";
    for (; !fc.atEnd(); fc.moveNext())
    {
        sItem = String( fc.item() );
        nLen = sItem.length;
        if( sItem.indexOf( "\\_" ) == -1 ){    // make sure folder name does not begin with _
            nTotal += EnumerateFolder( sItem );
        }
    }
    return nTotal;
}

function DoSomething( sPath )
{
    var y = 0;
    editor.OpenFile( sPath, 0, eeOpenAllowNewWindow );

    document.selection.Find( "(?<=<title>).+?(?=</title>)", eeFindNext | eeFindAround | eeFindReplaceRegExp | eeFindReplaceQuiet );
    sTitle = document.selection.Text;
    if( sTitle.length != 0 ){
        y = document.selection.GetTopPointY( eePosLogical );
    }
    else {
        y = document.selection.GetActivePointY( eePosLogical );
    }
    document.selection.Find( "(?<=<h2>).+?(?=</h2>)", eeFindNext | eeFindAround | eeFindReplaceRegExp | eeFindReplaceQuiet );
    sH = document.selection.Text;
    document.close();
    
    if( sH.substr( 0, 3 ) == "Q. " ){
        sH = sH.substr( 3 );
    }
    
    if( sTitle.length == 0 || sH.length == 0 || sTitle.indexOf( sH ) == -1 ){
        docResult.Activate(); 
        document.writeln( "" + sPath + "(" + y+1 + "):" );
        document.writeln( "\t<title> " + sTitle );
        document.writeln( "\t<h2>    " + sH );
        document.writeln( "" );
    }
}


----------------
Yutaka Emura
Developer of EmEditor
http://www.emeditor.com/

arnold
Posted on: 4/20/2009 5:26 pm
Just popping in
Joined: 3/27/2009
From:
Posts: 5
Re: Run macro for all files in a folder?
hi yutaka. first i want to ty for writing the script!
i tried this with one of the good scripts on this forum. i get a positive message, but also an error (ERRORS Root Folder: D:\test\) in a new file. and when i check the files, nothing happened here is the script i try to use in the test


sRootDir = "D:\\test\\";   // root folder
editor.NewFile(); 
var docResult = editor.ActiveDocument; 
docResult.writeln( "ERRORS Root Folder: " + sRootDir );

Redraw = false;
var nTotalFiles = EnumerateFolder( sRootDir );
Redraw = true;

alert( "Finished successfully. Total of " + nTotalFiles + " files processed." );


function EnumerateFolder( sFolder )
{
    var nTotal = 0;
    var fso, f, fc, s;
    fso = new ActiveXObject("Scripting.FileSystemObject");
    f = fso.GetFolder(sFolder);
    fc = new Enumerator(f.files);
    for (; !fc.atEnd(); fc.moveNext())
    {
        sItem = String( fc.item() );
        nLen = sItem.length;
        if( sItem.indexOf( "\\_" ) == -1 && sItem.substr( nLen - 4 ) == ".htm" ){  // make sure file or folder does not begin with _ and the file extension is .htm.
            DoSomething( sItem );
            nTotal++;
        }
    }

    fc = new Enumerator(f.SubFolders);
    s = "";
    for (; !fc.atEnd(); fc.moveNext())
    {
        sItem = String( fc.item() );
        nLen = sItem.length;
        if( sItem.indexOf( "\\_" ) == -1 ){    // make sure folder name does not begin with _
            nTotal += EnumerateFolder( sItem );
        }
    }
    return nTotal;
}

function DoSomething( sPath )
{
// helper function that we use to put the entire text of a file in a string
            // **************************************************
            function string_fromfile(sUrl){
                var ForReading = 1, ForWriting = 2, ForAppending = 8;
                var fso, fob;
                try{
                    fso = new ActiveXObject("Scripting.FileSystemObject");
                    fob = fso.OpenTextFile(sUrl, ForReading);
                    var strRead = fob.ReadAll();
                    fob.Close();
                    fso     = null;
                    return strRead;
                }
                catch(e){
                    if(000){}
                    else if(e.message == 'Input past end of file'){return '';}  // blank file
                    else if(e.message == 'File not found'){return ''}           // not found
                    else if(e.message != ''){return ''}                         // other problem
                };                 
            }        

            // main code
            // **************************************************            
            stext       =   (string_fromfile(document.FullName));
            smatch      =   '';
            
            // look inside the raw text and see if we can find something inside the h1 tag
            try{
                amatch      =   stext.match(/<h1>([^<]+)<.h1>/);
                smatch      =   amatch[1];
            }catch(ex){ smatch = '';}
            
            // did we actually find something inside the h1 tag?
            if(smatch != ''){
                // fix what we found so that unwanted characters get changed into a dash
                smatch  =   smatch.split(/[\W]+/).join('-');
                // extract the parent directory and combine it with what we found to form a new filename                
                newname = (document.FullName.split(/\x5c/).slice(0,-1).join("\x5c") + "\x5c" + smatch + '.htm');
                // save a copy of the document with its new name
                document.Save( newname );
            }
}
Yutaka
Posted on: 4/20/2009 8:51 pm
Webmaster
Joined: 9/28/2006
From: Redmond
Posts: 2398
Re: Run macro for all files in a folder?
"ERRORS Root Folder: D:\test\" does not really mean an error.

In your DoSomething( sPath ) function, you didn't even use sPath parameter passed to the function.


----------------
Yutaka Emura
Developer of EmEditor
http://www.emeditor.com/

Threaded | Newest First Previous Topic | Next Topic | Top


Register To Post
 
English čeština Deutsch español français italiano 日本語 한국어 Русский 简体中文 繁體中文