Remove Entry in INI File
Hi There,
I am not good with scripting and is struggling with an INI file. I want to check and remove a certain characters on string.
-------------------------------------
[MyINI]
YourINI=One,Two,Three,Four
-------------------------------------
I want to remove the "One" no matter what its position is and if it is the only entry, it will be blank:
-------------------------------------
[MyINI]
YourINI=
-------------------------------------
It could also be like this:
-------------------------------------
[MyINI]
YourINI=Two,Three,One,Four
-------------------------------------
I found the thread for VBCab which tackles it but still could not make my scripts work. Thank you!
I am not good with scripting and is struggling with an INI file. I want to check and remove a certain characters on string.
-------------------------------------
[MyINI]
YourINI=One,Two,Three,Four
-------------------------------------
I want to remove the "One" no matter what its position is and if it is the only entry, it will be blank:
-------------------------------------
[MyINI]
YourINI=
-------------------------------------
It could also be like this:
-------------------------------------
[MyINI]
YourINI=Two,Three,One,Four
-------------------------------------
I found the thread for VBCab which tackles it but still could not make my scripts work. Thank you!
0 Comments
[ + ] Show comments
Answers (2)
Please log in to answer
Posted by:
murali.bhat
13 years ago
Try this:
Option Explicit
Dim oFSO, sFile, oFile, sText, sNewText
Set oFSO = CreateObject("Scripting.FileSystemObject")
sFile = "<File Path>"
'On error resume next
If oFSO.FileExists(sFile) Then
Set oFile = oFSO.OpenTextFile(sFile, 1)
Do Until oFile.AtEndOfStream
sText = oFile.ReadLine
If Instr(1, Trim(sText), "YourINI") <> 0 Then
If Instr(1, Trim(sText), ",One,") <> 0 Then
sNewText = sNewText & vbcrlf & Replace(sText, ",One,",",")
elseif Instr(1, Trim(sText), "One,") <> 0 Then
sNewText = sNewText & vbcrlf & Replace(sText, "One,","")
elseif Instr(1, Trim(sText), "One") <> 0 Then
sNewText = sNewText & vbcrlf & Replace(sText, "One","")
end if
Else
sNewText = sNewText & vbcrlf & sText
End If
Loop
oFile.Close
Set oFile = oFSO.OpenTextFile(sFile, 2)
oFile.Write sNewText
End If
Set oFSO = Nothing
Option Explicit
Dim oFSO, sFile, oFile, sText, sNewText
Set oFSO = CreateObject("Scripting.FileSystemObject")
sFile = "<File Path>"
'On error resume next
If oFSO.FileExists(sFile) Then
Set oFile = oFSO.OpenTextFile(sFile, 1)
Do Until oFile.AtEndOfStream
sText = oFile.ReadLine
If Instr(1, Trim(sText), "YourINI") <> 0 Then
If Instr(1, Trim(sText), ",One,") <> 0 Then
sNewText = sNewText & vbcrlf & Replace(sText, ",One,",",")
elseif Instr(1, Trim(sText), "One,") <> 0 Then
sNewText = sNewText & vbcrlf & Replace(sText, "One,","")
elseif Instr(1, Trim(sText), "One") <> 0 Then
sNewText = sNewText & vbcrlf & Replace(sText, "One","")
end if
Else
sNewText = sNewText & vbcrlf & sText
End If
Loop
oFile.Close
Set oFile = oFSO.OpenTextFile(sFile, 2)
oFile.Write sNewText
End If
Set oFSO = Nothing
Posted by:
anonymous_9363
13 years ago
I can't see what the difficulty is in using the INI class but...whatever...
As to the posted script (and for any script using the class, if you return to that), it'd be simpler to use Split using the 'equals' sign as the separator, then Split again using comma as the separator, then testing for the string. You can then reassemble the string using the array elements.
As to the posted script (and for any script using the class, if you return to that), it'd be simpler to use Split using the 'equals' sign as the separator, then Split again using comma as the separator, then testing for the string. You can then reassemble the string using the array elements.
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
so that the conversation will remain readable.