我正在编写一个编辑现有.ini文件(nsapps.ini)的脚本。一切都运作良好但我不知道如何在脚本中添加一个检查,如果3条线存在就会停止添加线
EpicTer78Class=9
EpicTer79Class=9
EpicTer81Class=9
EpicTer78Class = 9是ini中的现有行,其他2行应该在它之后添加。我想添加这个检查,所以如果脚本意外运行更多,那么一旦它不会添加多个线路的coppies。感谢您的帮助(就此而言,我是超级棒)。
'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
答案 0 :(得分: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