3

我正在尝试编写一个 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"
4

1 回答 1

2

您正在LCASE对字符串进行转换,然后在所有大写字母中查找字符串。

将代码更改为:
If (InStr(strLine, "EXTMGR_ADDINS") > 0)

于 2012-11-16T21:29:47.837 回答