3
我正在尝试编写一个VBS脚本以从.ini文件中删除一行。 但是,当我运行它时,新文件(以及备份)被创建并重命名,但是我想删除的行仍然存在? 我该如何解决这个问题?如何使用VBS脚本从.ini文件中删除一行
这里是我的代码:
Const ForReading = 1
Const ForWriting = 2
Const OpenAsASCII = 0
Const CreateIfNotExist = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const OverwriteExisting = True
'Making a backup of the file
objFSO.CopyFile "C:\notes.ini" , "C:\notesBACKUP.ini"
'Setting input of file
strInput = "C:\notes.ini"
Set objInput = objFSO.OpenTextFile(strInput, ForReading)
'Setting temp output for new file with omitted line
strOutput = "C:\notes2.ini"
Set objOutput = objFSO.OpenTextFile(strOutput, _
ForWriting, CreateIfNotExist, OpenAsASCII)
Do Until objInput.AtEndOfStream
strLine = objInput.ReadLine
'Line with EXTMGR to be replaced when copying to new file
If (InStr(LCase(strLine), "EXTMGR") > 0) Then
'New line replacing old one
strLine = "#Deleted"
End If
objOutput.WriteLine strLine
Loop
objInput.Close
objOutput.Close
'Deleting the original file
objFSO.DeleteFile(strInput)
'Renaming the new file (with line removed) to the original filename
objFSO.MoveFile "C:\notes2.ini" , "C:\notes.ini"
你是做一个字符串的LCASE转换,然后寻找在全部大写的字符串 - 似乎很奇怪。您想要替换的INI文件中的行是什么?你能分享一下吗? – Andrew
true - 没有注意到,正在使用来自旧脚本的部分 - 这里是我需要注释(或删除)的行: EXTMGR_ADDINS = NCExtMgr – Bajan
If(InStr(strLine,“EXTMGR_ADDINS”)> 0)这不行吗? – Andrew