1

我正在编写一个编辑现有 .ini 文件 (nsapps.ini) 的脚本。一切正常,但我不知道如何在脚本中添加一个检查,如果有 3 行,它将停止添加行

EpicTer78Class=9
EpicTer79Class=9
EpicTer81Class=9

EpicTer78Class=9 是 ini 中的现有行,其他 2 行应该在它之后添加。我想添加此检查,因此如果脚本意外运行得更多,那么一旦它不会添加多个行副本。感谢您的帮助(在这方面我是个超级菜鸟)。

 'Begin Code for script

Const ForReading = 1
Const ForWriting = 2

'Source file name here
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\ProgramData\Nuance\NaturallySpeaking10\nsapps.ini", ForReading)

Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
'Line to find (changes go under this and replace line)
    intLineFinder = InStr(strNextLine, "EpicTer78Class=9")
    If intLineFinder <> 0 Then
    'This is what replaces first line (above)
        strNextLine = "EpicTer78Class=9" & vbCrLf & "EpicTer79Class=9" & vbCrLf & "EpicTer81Class=9"
    End If

    strNewFile = strNewFile & strNextLine & vbCrLf
Loop

objTextFile.Close
'Source of file goes here again
Set objTextFile = objFSO.OpenTextFile("C:\ProgramData\Nuance\NaturallySpeaking10\nsapps.ini", ForWriting)

objTextFile.WriteLine strNewFile
objTextFile.Close
4

1 回答 1

2

假设这三行应该以给定的顺序出现,我建议这样:

inifile = "C:\ProgramData\Nuance\NaturallySpeaking10\nsapps.ini"

Set fso = CreateObject("Scripting.FileSystemObject")

Set inFile  = fso.OpenTextFile(inifile)
Set outFile = fso.OpenTextFile(inifile & ".new", 2, True)

prev = ""
Do Until inFile.ATEndOfStream
  curr = Trim(inFile.ReadLine)
  If prev = "EpicTer78Class=9" And curr <> "EpicTer79Class=9" Then
    outFile.WriteLine "EpicTer79Class=9"
    prev = "EpicTer79Class=9"
  End If
  If prev = "EpicTer79Class=9" And curr <> "EpicTer81Class=9" Then
    outFile.WriteLine "EpicTer81Class=9"
    prev = "EpicTer81Class=9"
  End If
  outFile.WriteLine curr
  prev = curr
Loop

inFile.Close
outFile.Close

fso.DeleteFile inifile, True
fso.MoveFile inifile & ".new", inifile
于 2013-10-10T20:21:25.283 回答