0

我在此发布了另一个,但我不得不对其进行大量编辑...

最重要的是批处理(可能包括VBScript)可以在txtfile中找到第29行......它应该编辑这一行。

如果第 29 行是这样的:'option=21' 它应该将其更改为 'option=22'

问题是该行更多地位于文件中。所以它应该只编辑第 29 行...

如何???

[请不要自定义程序;;; 它应该由每个用户完成,而无需安装任何东西。]

4

3 回答 3

3

这不是您通常批量执行的操作,但它相当简单:

@echo off
setlocal enableextensions enabledelayedexpansion

rem the input file
set inputfile=file.txt

rem temporary file for output, we can't just write into the
rem same file we're reading
set tempfile=%random%-%random%.tmp

rem delete the temporary file if it's already there
rem shouldn't really happen, but just in case ...
copy /y nul %tempfile%

rem line counter
set line=0

rem loop through the file
for /f "delims=" %%l in (%inputfile%) do (
    set /a line+=1
    if !line!==29 (
        rem hardcoded, at the moment, you might want to look
        rem here whether the line really starts with "options"
        rem and just put another number at the end.
        echo option=22>>%tempfile%
    ) else (
        echo %%l>>%tempfile%
    )
)

del %inputfile%
ren %tempfile% %inputfile%

endlocal

如果您想调整它,它应该为您指明大方向。

于 2009-07-12T10:18:36.207 回答
0

我看到你在要求一个vb脚本,
使用AWK,它会像这样,
也许它会帮助你编码vb

awk '{if(FNR==29) {gsub(/option=21/,"option=22"); print} else {print $0;}}' input.txt > output.txt

没试过,所以可能有一些轻微的故障......

  1. 遗嘱仅在第 29 行 检查FNR=29处理gsub
    • gsuboption=21option=22
    • 其他线路将通过
于 2009-07-12T10:06:55.387 回答
0

这是一个 vbscript

Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\file.txt"
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfStream
    linenum = objFile.Line
    strLine = objFile.ReadLine
    If linenum = 29 Then
        strLine = Replace(strLine,"option=21","option=22")
    End If
    WScript.Echo strLine
Loop    

如何使用:

c:\test> cscript /nologo myscript.vbs > newfile
c:\test> ren newfile file.txt
于 2009-07-12T11:04:54.460 回答