如何使用批处理在文本文件中的某一行文本后添加非固定行?

问题描述 投票:0回答:3

我正在尝试自动将某一行文本添加到文本文件中。例如,在“~block 2~”段落的最后一行下添加这一行“Script#3=script3”。但是,“~block 2~”段落中的项目数量可能会有所不同。

"~block 1~"

Script#1=script1

Script#2=script2

"~block 2~"

Script#1=script1

Script#2=script2

"~block 3~"

Script#1=script1

Script#2=script2

"~block 1~"

Script#1=script1

Script#2=script2

"~block 2~"

Script#1=script1

Script#2=script2

Script#3=script3

"~block 3~"

Script#1=script1

Script#2=script2

我尝试编写代码,但似乎不起作用。

@echo OFF

SET /A CHECK = 0
SET /A a = 1
(
FOR /F "tokens=*" %%A IN (C:\Users\Desktop\testBatchtestModify.txt) DO (
   ECHO %%A

   IF %CHECK%==0 (
    ECHO %%A
   )
   ELSE (
    IF "%%A" EQU "" (
       ECHO "Script#" %CHECK% "=TEST"
       ECHO 
       SET /A CHECK = 0
    )
    ELSE (
       SET /A CHECK = %CHECK%+%a%
       ECHO %%A
    )
   )

   IF "%%A" EQU "~block 2~" (
    SET /A CHECK = 1
   )
) >> temp.txt
move /y temp.txt C:\Users\Desktop\testBatchtestModify.txt

batch-file
3个回答
0
投票
@ECHO OFF
SETLOCAL
rem The following settings for the source directories and filenames are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"
SET "destdir=u:\your results"
SET "filename1=%sourcedir%\q74927660.txt"
SET "outfile=%destdir%\outfile.txt"

SET "insertme=Script#3=script3"
SET "atendofblock=~block 2~"
SET "blockstart="

(
FOR /f "usebackqdelims=" %%e IN ("%filename1%") DO (
 IF "%%~e" neq "%%e" (
  IF DEFINED blockstart IF DEFINED insertme (ECHO %insertme%&SET "insertme=")
  SET "blockstart="
 )
 IF "%%~e"=="%atendofblock%" SET "blockstart=y"
 ECHO %%e
)
IF DEFINED insertme IF DEFINED blockstart ECHO %insertme%&SET "insertme="
)>"%outfile%"

IF DEFINED insertme ECHO "%atendofblock%" NOT found - no CHANGE made

GOTO :EOF

在应用于真实数据之前,始终验证测试目录。

请注意,如果文件名不包含空格等分隔符,则

usebackq
%filename1%
周围的引号都可以省略。

读取文件的每一行。

如果该行与块末尾名称匹配,请将

blockstart
设置为 something (它已被定义)

如果该行被引用,那么我们就有了一个新块,因此,如果设置了

blockstart
,则输出“插入”行并将
blockstart
设置为nothing
blockstart
变为未定义)


0
投票

这个更简单方法不会在块2末尾插入行,而是在块3之前插入

;)

@echo off
setlocal EnableDelayedExpansion

for /F "delims=:" %%a in ('findstr /N /L /C:"~block 3~" test.txt') do set /A "lines=%%a-1"

< test.txt (
   for /L %%a in (1,1,%lines%) do (
      set "line="
      set /P "line="
      echo(!line!
   )
   echo Script#3=script3
   echo/
   findstr "^"
) > temp.txt

0
投票

针对已发布示例的此行插入任务的工作批处理文件,其中包含空行以及带有

"
~
的行的文本文件是:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "TextFile=C:\Users\Desktop\testBatchtestModify.txt"
if not exist "%TextFile%" exit /B
(for /F delims^=^ eol^= %%I in ('%SystemRoot%\System32\findstr.exe /N "^" "%TextFile%" 2^>nul') do (
    set "Line=%%I"
    setlocal EnableDelayedExpansion
    if not "!Line:block 3=!" == "!Line!" echo Script#3=script3& echo(
    echo(!Line:*:=!
    endlocal
))>"%TextFile%.tmp"
move /Y "%TextFile%.tmp" "%TextFile%" >nul
if exist "%TextFile%.tmp" del "%TextFile%.tmp"
endlocal

主要的 FOR 循环参见 如何逐行读取和打印文本文件的内容?

从文件中读取的每一行也会未经修改地输出,并写入临时文件中,该文件在运行 FOR 循环之前打开一次,并在所有行写入文件后关闭一次。

IF 条件检查当前行是否包含不区分大小写的字符串

block 3
,在这种情况下,会输出带有
Script#3=script3
的行,并在带有
block 3
的行之前输出一个空行。

带有附加行的临时文件将替换原始文件。

要了解所使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完整、仔细地阅读每个命令显示的帮助页面。

  • del /?
  • echo /?
  • endlocal /?
  • findstr /?
  • for /?
  • if /?
  • move /?
  • set /?
  • setlocal /?
© www.soinside.com 2019 - 2024. All rights reserved.