You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在EmEditor中按文件批量执行200+替换项?

Efficiently Implement Per-File Batch Replace in EmEditor 19.9.1

I get it—you're stuck with 200+ replace items and a huge folder of files, and EmEditor's default Batch Replace All runs each replacement across every file first instead of processing one file fully before moving to the next. Your initial macro works but is painfully slow because opening each file in the editor adds 50ms of overhead per file. Let's fix this with two optimized approaches tailored to your needs.

Method 1: Offline Text Replacement (Max Speed, No Editor Window Overhead)

The biggest bottleneck in your original macro is opening each file in the EmEditor UI. Instead, we'll read file content directly into memory, run all replacements on the text string, and only write changes back if necessary. This cuts out all UI-related overhead and speeds things up drastically.

Here's the optimized macro code:

var fso = new ActiveXObject("Scripting.FileSystemObject");
var adodbStream = new ActiveXObject("ADODB.Stream");
var files = [];
var filesHasModified = [];

// Helper: Read file with proper encoding (adjust charset to match your files)
function readFile(filePath) {
    adodbStream.Type = 2; // Text mode
    adodbStream.Charset = "UTF-8"; // Use "GB2312" for GBK, "UTF-16" for Unicode, etc.
    adodbStream.Open();
    adodbStream.LoadFromFile(filePath);
    var content = adodbStream.ReadText();
    adodbStream.Close();
    return content;
}

// Helper: Write modified content back to file
function writeFile(filePath, content) {
    adodbStream.Type = 2;
    adodbStream.Charset = "UTF-8"; // Match the read encoding to avoid garbled text
    adodbStream.Open();
    adodbStream.WriteText(content);
    adodbStream.SaveToFile(filePath, 2); // Overwrite existing file
    adodbStream.Close();
}

function showFolderFileList(folderspec) {
    var f = fso.GetFolder(folderspec);
    // Skip excluded directories
    if(/\\(doc|lib|\.git|\.idea|\.vs|dll)$/gi.test(folderspec)){
        return;
    }
    // Recurse subfolders
    var subfolders = new Enumerator(f.SubFolders);
    for(; !subfolders.atEnd(); subfolders.moveNext()) {
        showFolderFileList((subfolders.item()).path);
    }
    // Collect target files
    var fc = new Enumerator(f.files);
    for (; !fc.atEnd(); fc.moveNext()) {
        var file = fc.item();
        if(/\.(jmx|config|cs|Config|tt|ttinclude|txt|yml|java|bak|xml|cshtml|sh|yaml|js|json|md|properties)$/gi.test(file.Name)){
            files.push(file.Path);
        }
    }
}

// Start processing
showFolderFileList('D:\\Sources.git2');

for(var i = 0; i < files.length; i++) {
    var filePath = files[i];
    var originalContent = readFile(filePath);
    var modifiedContent = originalContent;

    // Run all your replace operations here
    modifiedContent = modifiedContent.replace(/http:\/\/172xx/g, "http://offline.esb.xx");
    modifiedContent = modifiedContent.replace(/http:\/\/172xx1/g, "http://offline.esb.xx");
    modifiedContent = modifiedContent.replace(/http:\/\/172xx2/g, "http://offline.esb.xx");
    // ... add your remaining 200+ replace items here

    // Only write back if content actually changed (reduces disk IO)
    if(modifiedContent !== originalContent) {
        writeFile(filePath, modifiedContent);
        filesHasModified.push(filePath);
    }
}

// Show list of modified files
editor.NewFile();
document.selection.Text = filesHasModified.join("\r\n");

Key Improvements:

  • No Editor Windows: We use ADODB.Stream to read/write files directly, skipping EmEditor's document UI entirely. This eliminates the 50ms per file overhead from opening files in the editor.
  • Memory-Based Replacements: All edits happen on a string in memory, which is far faster than interacting with the editor's selection API.
  • Smart IO: We only write to disk if the content changed, reducing unnecessary disk operations.
  • Encoding Control: The ADODB.Stream lets you specify exact file encoding, preventing garbled text (adjust the Charset value to match your files' actual encoding).

Method 2: Optimized Editor Window Approach (For Advanced Features)

If you need to use EmEditor's advanced features (like syntax-aware replacements or editor-specific regex flags), you can optimize your original macro to cut down on overhead:

var fso = new ActiveXObject("Scripting.FileSystemObject");
var files = [];
var filesHasModified = [];

// Disable UI redraw to speed up processing
editor.EnableRedraw = false;

function showFolderFileList(folderspec) {
    // ... same folder traversal code as your original macro ...
}

showFolderFileList('D:\\Sources.git2');

// Define all replace pairs in an array for cleaner code
var replacePairs = [
    ["http://172xx", "http://offline.esb.xx"],
    ["http://172xx1", "http://offline.esb.xx"],
    ["http://172xx2", "http://offline.esb.xx"],
    // ... add all 200+ pairs here
];

for( i in files) {
    var file = files[i];
    // Open file without activating it to save UI overhead
    editor.OpenFile(file.Path, eeOpenNoActivate | eeOpenDetectAll);
    
    var hasChanges = false;
    // Run all replacements for the current file
    for(var j = 0; j < replacePairs.length; j++) {
        var replaceCount = document.selection.Replace(replacePairs[j][0], replacePairs[j][1], eeReplaceAll, 0);
        if(replaceCount > 0) hasChanges = true;
    }
    
    if(hasChanges) {
        filesHasModified.push(file.Path);
        document.Save();
    }
    // Close without prompt (we already saved if needed)
    document.Close(eeSaveNo);
}

// Re-enable UI redraw
editor.EnableRedraw = true;

// Show modified files list
editor.NewFile();
document.selection.Text = filesHasModified.join("\r\n");

Key Optimizations Here:

  • Disable Redraw: Prevents the editor from updating the UI while processing files, which saves significant time.
  • Background File Opening: Uses eeOpenNoActivate to open files without bringing them to the foreground.
  • Cleaner Replace Logic: Groups replace pairs into an array for easier maintenance.

Which Method to Choose?

  • Go with Method 1 for maximum speed (it’s easily 10-20x faster than opening editor windows) if you’re doing simple text/regex replacements.
  • Use Method 2 if you need EmEditor’s unique features (like syntax-aware edits or macro commands that rely on the document object).

内容的提问来源于stack exchange,提问作者yongfa365

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 08:32:53