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