求大佬帮忙搞个bat处理txt里的内容?-灵析社区

577739990

各位大佬下午好,请问要怎么通过bat文件处理txt的文本内容。 现在的情况是我通过notepad++的文本工具处理txt文件内容。 以下是我的手动操作步骤。 1、我通过正则把 txt中的文本每行 字数进行限制代码如下: 替换目标:(.{6,18}[,。:!?]|.{16}) 替换为:$1\n 2、然后继续通过替换,把txt文本中的“,。!” 等内容替换为空。 3、然后在移除空行。 4、至此完成一个txt文档的处理工程 每天要重复处理几百个txt文档,请问,怎么批量操作以上的步骤呢,bat能实现拖入txt文档自动执行这些步骤嘛。 求大佬帮忙解脱这些机械式的工作。

阅读量:298

点赞量:19

问AI
听取楼下的改进版本: @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 替换成你的输入和输出文件路径就行。