在cmd中的txtfile中间添加一行 [英] Add a line in middle of txtfile in cmd

查看:688
本文介绍了在cmd中的txtfile中间添加一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个文本文件 txtfile.txt

  line1 
line2
在此行后面添加
line4
etc
etc

我想创建一个批处理文件,可以在后添加一行,在此行后面添加和用户输入的信息。

例如:

 输入信息:asdf 

/ p>

  line1 
line2
在此行后面添加
asdf
line4
etc
etc

我认为基本过程是循环文件复制它,当我找到线,添加线,然后循环通过其余。我想知道如何做。

解决方案

  @ECHO OFF 
SETLOCAL
SET / p info =enter info:
:: read addafter line

FOR / fdelims =%% i IN(poison1.txt)DO (
SET addafter = %% i
FOR / fdelims =%% n IN('findstr / n^txtfile.txt)DO(
SET line = %% n
SETLOCAL ENABLEDELAYEDEXPANSION
SET line =!line:*:=!
ECHO(!line!
如果!line!==!addafter! info%
ENDLOCAL


)> newfile.txt

FC newfile.txt txtfile.txt

GOTO:eof

其中 poison1.txt 一行

 A line!of!] many<&> var * ied%poison ^ char; ac (ters)| like,a\\\
d + so = on

和txtfile.txt




  • 读取 poison1.txt 文件中的行到 addafter

  • 对于添加到我的文件中的每一行



    • 删除号码和第一个冒号(由 FINDSTR 添加)

    • 如果行与目标匹配,则输出该行

    • 如果行与目标匹配,则输出额外行



完成!


here's a text file txtfile.txt

line1
line2
add after this line
line4
etc
etc

i want to create a batch file which can add a line after add after this line with the user entered info. keeping the add after this line intact.

example:

enter info: asdf

and the file becomes

line1
line2
add after this line
asdf
line4
etc
etc

i think that the basic process would be to loop through the file copying it and when i find the line, adding the line and then looping through the rest. i want to know how to do it.

解决方案

@ECHO OFF
SETLOCAL
SET /p info="enter info : "
:: read addafter line
(
FOR /f "delims=" %%i IN (poison1.txt) DO (
SET addafter=%%i
FOR /f "delims=" %%n IN (' findstr /n "^" txtfile.txt') DO (
SET line=%%n
SETLOCAL ENABLEDELAYEDEXPANSION
SET line=!line:*:=!
ECHO(!line!
IF "!line!"=="!addafter!" ECHO(%info%
ENDLOCAL
)
)
)>newfile.txt

FC newfile.txt txtfile.txt

GOTO :eof

Where poison1.txt contains the one line

"A line !of! ] many < & >var*ied %poison ^ char;ac(ters) | like "," a\nd+so=on"

and txtfile.txt contains this line.

  • Read the line from the poison1.txt file to addafter
  • For each line in the add-to-me file
    • Number the line to catch empty lines
    • Remove the number and first colon (added by FINDSTR)
    • Output the line verbatim
    • if the line matches the target, output the extra line

done!

这篇关于在cmd中的txtfile中间添加一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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