听取楼下的改进版本:
@echo off
setlocal enabledelayedexpansion
:: 设置文件夹路径
set "folderPath=your_folder_path"
:: 遍历文件夹里的所有 txt 文件
for %%F in (%folderPath%\*.txt) do (
set "inputFile=%%F"
set "outputFile=%%F_temp.txt"
:: 第一步:用正则表达式限制每行的字符数并添加新行
powershell -Command "(Get-Content '!inputFile!') -replace '(.{6,18}[,。:!?]|.{16})', '$1`n' | Set-Content '!outputFile!'"
:: 第二步:替换特定字符为空
powershell -Command "(Get-Content '!outputFile!') -replace '[,。!]', '' | Set-Content '!outputFile!'"
:: 第三步:移除空行
powershell -Command "(Get-Content '!outputFile!') | ? {$_ -ne ''} | Set-Content '!outputFile!'"
:: 替换原文件
move /Y "!outputFile!" "!inputFile!"
echo 文件 %%F 处理完成!
)
echo 所有文件处理完成!
pause
原方法:
写一个批处理脚本(.bat文件)
@echo off
setlocal enabledelayedexpansion
:: 设置输入和输出文件路径
set "inputFile=your_input_file.txt"
set "outputFile=your_output_file.txt"
:: 第一步:用正则表达式限制每行的字符数并添加新行
powershell -Command "(Get-Content '%inputFile%') -replace '(.{6,18}[,。:!?]|.{16})', '$1`n' | Set-Content '%outputFile%'"
:: 第二步:替换特定字符为空
powershell -Command "(Get-Content '%outputFile%') -replace '[,。!]', '' | Set-Content '%outputFile%'"
:: 第三步:移除空行
powershell -Command "(Get-Content '%outputFile%') | ? {$_ -ne ''} | Set-Content '%outputFile%'"
echo 文件处理完成!
pause
然后你可以拖你的 txt 文件到这个 .bat 文件上来处理,把 your_input_file.txt 和 your_output_file.txt
替换成你的输入和输出文件路径就行。