编写一个批处理文件来获取文本文件的目录/搜索特定的记号,之后插入一行 [英] write a batch file to get a directory of text files/ search for a specific token and insert a line after that

查看:140
本文介绍了编写一个批处理文件来获取文本文件的目录/搜索特定的记号,之后插入一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从文件夹中读取一堆文本文件。找到一个特定的标记,如花冠每个文本文件,并插入令牌后的新行。
假设文本文件是这样的:


第一行
第二行
第三行,卡罗拉的一部分,第三行剩余
插入新的LINE HERE
文件的其余部分。

我很新的批处理文件,这对我来说是挑战。

感谢

部分答案:

我可以用下面的code,但这code只能找到令牌,如果整条生产线有标记。我还需要找到令牌,如果它是一些句子之间以及(子)!

 关闭@echo
设置SrcFolder = C:\\输入
设置DstFolder = C:\\输出
对于一个%%在(%SrcFolder%\\ *。TXT),做(
  在(%% A)做(FOR / F有usebackq delims =%% H(
    呼应。%% ^ h
    如果%% HEQU花冠(
      回声新线
    )
  ))>中%DstFolder%\\ %%〜NXA


解决方案

在地方

 %% HEQU花冠(

(这无论如何都应该产生一个错误)

 回声%% H | FINDSTR花冠> NUL&放大器;如果没有ERRORLEVEL 1(

这会在每行的文字花冠,如果发现设置错误级别 0 。不要试图如果错误级别0 ,因为 FINDSTR 总是设置错误级别为正整数且如果错误级别试验测试错误级别为n 或大于n

I need to read a bunch of text files from a folder. find a specific token such as "Corolla" for each text file and insert a new line after the token. Suppose the text file is like:

first line
second line
a part of third line, Corolla, third line remaining
INSERT THE NEW LINE HERE
the rest of file. 

I am very new to batch files and it is challenging for me.

Thanks

Partial Answer:

I could use the following code, but this code only finds the token if the whole line has that token. I also needs to find the token if it is between some sentences as well (substring)!

@echo off
set SrcFolder=C:\Input
set DstFolder=C:\Output
for %%a in ("%SrcFolder%\*.txt") do (
  (for /f "usebackq delims=" %%h in ("%%a") do (
    echo.%%h
    if "%%h" equ "Corolla" (
      echo THE NEW LINE
    )
  ))>"%DstFolder%\%%~nxa"
)

解决方案

In place of

     "%%h" equ "Corolla" (

(which should yield an error anyway)

put

 echo %%h|findstr "Corolla" >nul&if not errorlevel 1 (

which looks for the literal "Corolla" in each line, setting errorlevel to 0 if found. Do not try if errorlevel 0 because findstr will always set errorlevel to a positive integer and the if errorlevel test tests for errorlevel being n or greater than n

这篇关于编写一个批处理文件来获取文本文件的目录/搜索特定的记号,之后插入一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆