在第17行的txt文件中批量写入文本

时间:2022-04-25 02:23:10

I want to make a program that write/replaces text at line 17 in a .txt file. I tried out this, but its only working for two lines

我想在.txt文件中创建一个在第17行编写/替换文本的程序。我试过这个,但它只用于两行

@Echo off
Echo hello >>>>>>>>>>>>>>>>> test.txt

Its outcome is:

其结果是:

">> was unexpected at this time."

But this work just fine:

但这项工作很好:

@Echo off
Echo hello >> test.txt

2 个解决方案

#1


@echo off

(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" test.txt') do (
   if %%a neq 17 (
      echo(%%b
   ) else (
      echo The new line 17
   )
)) > output.txt
move /Y output.txt test.txt

This solution eliminate the colons that may appear at beginning of the line. This problem may be fixed, if needed.

此解决方案消除了可能出现在行开头的冒号。如果需要,可以修复此问题。

#2


Set Inp = WScript.Stdin
Set Outp = Wscript.Stdout

For x = 1 to 16
    OutP.WriteLine Inp.Readline
Loop
Inp.Readline
OutP.WriteLine wscript.Arguments(0)
Do Until Inp.AtEndOfStream
    OutP.WriteLine Inp.Readline
Loop

Name it something.vbs

将它命名为something.vbs

to use

cscript //nologo something.vbs "Line 17 text" < infile.txt > outfile.txt

#1


@echo off

(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" test.txt') do (
   if %%a neq 17 (
      echo(%%b
   ) else (
      echo The new line 17
   )
)) > output.txt
move /Y output.txt test.txt

This solution eliminate the colons that may appear at beginning of the line. This problem may be fixed, if needed.

此解决方案消除了可能出现在行开头的冒号。如果需要,可以修复此问题。

#2


Set Inp = WScript.Stdin
Set Outp = Wscript.Stdout

For x = 1 to 16
    OutP.WriteLine Inp.Readline
Loop
Inp.Readline
OutP.WriteLine wscript.Arguments(0)
Do Until Inp.AtEndOfStream
    OutP.WriteLine Inp.Readline
Loop

Name it something.vbs

将它命名为something.vbs

to use

cscript //nologo something.vbs "Line 17 text" < infile.txt > outfile.txt