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

多文件夹多文件日期批量替换:Notepad++宏无法实现,求自动化方案

批量替换多文件夹下XML/TXT文件中的日期格式

方法一:Notepad++ 批量正则替换(无需录制宏)

Notepad++自带的「在文件中查找替换」功能结合正则表达式,就能一次性处理三种日期格式:

  1. 打开Notepad++,点击顶部菜单「搜索」→「在文件中查找」
  2. 在弹窗中配置:
    • 「查找模式」选「正则表达式」,勾选「匹配大小写」
    • 「查找内容」输入覆盖三种格式的正则:
      (2023-04-10)|(<strong>20230410</strong>)|(<strong>230410</strong>)
      
    • 「替换为」输入对应替换规则,用分组匹配对应替换:
      (?12023-05-24)(?2<strong>20230524</strong>)(?3<strong>230524</strong>)
      
    • 「目录」选目标根文件夹,勾选「包括子文件夹」,「过滤器」填 *.xml;*.txt
    • 点击「全部替换」完成批量操作

正则说明:括号将三种日期格式分为3组,(?1...) 表示匹配到第1组时替换为指定内容,以此类推,一次完成多格式替换。

方法二:Python脚本(灵活可控,适配复杂场景)

如果需要频繁修改日期或扩展格式,用Python脚本更灵活:

import os
import re

# 替换规则:(匹配正则, 替换内容)
replace_rules = [
    (re.compile(r'2023-04-10'), '2023-05-24'),
    (re.compile(r'(<strong>)20230410(</strong>)'), r'\120230524\2'),
    (re.compile(r'(<strong>)230410(</strong>)'), r'\1230524\2')
]

# 目标文件夹路径,修改为你自己的路径
root_dir = r'C:\Your\Target\Folder'
# 要处理的文件后缀
target_extensions = ('.xml', '.txt')

def update_file_content(file_path):
    with open(file_path, 'r', encoding='utf-8') as f:
        content = f.read()
    # 应用所有替换规则
    for pattern, replacement in replace_rules:
        content = pattern.sub(replacement, content)
    # 写回文件
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(content)

# 遍历所有子文件夹和目标文件
for dirpath, _, filenames in os.walk(root_dir):
    for filename in filenames:
        if filename.endswith(target_extensions):
            full_path = os.path.join(dirpath, filename)
            update_file_content(full_path)
            print(f"处理完成:{full_path}")

print("所有文件处理完毕!")

使用步骤:

  • 修改 root_dir 为你的目标文件夹路径
  • 如需更换日期,直接修改 replace_rules 中的匹配和替换内容
  • 保存为 replace_dates.py,运行脚本(需安装Python环境)

方法三:PowerShell脚本(Windows原生,无需额外安装)

Windows用户可以用原生PowerShell完成,不用装其他工具:

# 替换规则集合
$replaceRules = @(
    @{ Pattern = '2023-04-10'; Replacement = '2023-05-24' },
    @{ Pattern = '(<strong>)20230410(</strong>)'; Replacement = '$120230524$2' },
    @{ Pattern = '(<strong>)230410(</strong>)'; Replacement = '$1230524$2' }
)

# 目标文件夹路径,修改为你自己的路径
$rootDir = "C:\Your\Target\Folder"
# 获取所有目标文件
$targetFiles = Get-ChildItem -Path $rootDir -Recurse -Include *.xml,*.txt

foreach ($file in $targetFiles) {
    # 读取文件内容
    $content = Get-Content -Path $file.FullName -Raw -Encoding UTF8
    # 应用所有替换规则
    foreach ($rule in $replaceRules) {
        $content = $content -replace $rule.Pattern, $rule.Replacement
    }
    # 写回文件
    Set-Content -Path $file.FullName -Value $content -Encoding UTF8
    Write-Host "处理完成:$($file.FullName)"
}

Write-Host "所有文件处理完毕!"

使用步骤:

  • 修改 $rootDir 为你的目标文件夹路径
  • 保存为 ReplaceDates.ps1,右键选择「用PowerShell运行」

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 17:47:04