bat在特定行号处添加行

时间:2010-10-01 04:22:37

标签: windows batch-file

我正在采用这种方法在现有文件的顶部添加一行SOME TEXT TO BE ADDED。有没有办法指定添加新文本的确切行号。例如,在最后一行之前(并添加换行符)或在第三行之后(并添加换行符)

copy original.txt temp.txt
echo.SOME TEXT TO BE ADDED>original.txt
type temp.txt >>original.txt
del temp.txt

1 个答案:

答案 0 :(得分:4)

VBSCRIPT

strLineNum = WScript.Arguments(0)
strAddText= WScript.Arguments(1)
strFileName = WScript.Arguments(2)
Set objFS = CreateObject( "Scripting.FileSystemObject" )
Set objFile = objFS.OpenTextFile(strFileName)
Do Until objFile.AtEndOfStream
    linenum=objFile.Line
    strLine = objFile.ReadLine
    If linenum = CInt(strLineNum) Then
        WScript.Echo strAddText
    End If
    WScript.Echo strLine
Loop       
objFile.Close

用法:

C:\test> cscript //nologo myscript.vbs 2 "text to insert" file >temp
C:\test> ren temp file