JadeKnight
IS-IT--Management
I'm a bit stuck here, could need some help...
I've got one application wich need some entries in a txt file. I've written a script wich check existing file, read all lines into an array, and check if old entries exist. If found, entry is checked. Then it copies the array to a new one, filtering out the checked entries. This works great, however, when I'm writing the new txt file. I also recieve four blank lines in the file. This matches the number of entries deleted. I assume this happends because the both arrays contain same elements. How can I correct this?
a bit of the code
I've got one application wich need some entries in a txt file. I've written a script wich check existing file, read all lines into an array, and check if old entries exist. If found, entry is checked. Then it copies the array to a new one, filtering out the checked entries. This works great, however, when I'm writing the new txt file. I also recieve four blank lines in the file. This matches the number of entries deleted. I assume this happends because the both arrays contain same elements. How can I correct this?
a bit of the code
Code:
'Set path to Services
Set WshShell=Wscript.CreateObject("Wscript.Shell") 'Initiate WSH
strSystemRoot = WshShell.ExpandEnvironmentStrings("%SystemRoot%") 'Get SystemRoot
strServices = strSystemRoot & "\system32\drivers\etc\services" 'Services file
'Reading Services into Array
Set fso = CreateObject("Scripting.FileSystemObject") 'Initiate FSO
Set objServices = fso.OpenTextFile(strServices,ForReading) 'Open the file for reading
objArr = Split(objServices.ReadAll, vbNewLine) 'Read file, split every line into array
objServices.Close 'Close the file
Set objServices = Nothing
'CheckFlag
strCheck = 1
'Parsing Array checking Old entries, if found --> mark for delete
For i = LBound(objArr) to UBound(objArr)
oldStr=LCase(objArr(i))
If Left(oldstr,7)="odbcvar" Then
objArr(i)="Delete"
strCheck = 2
End If
If Left(oldstr,6)="rpcvar" Then
objArr(i)="Delete"
strCheck = 2
End If
If Left(oldstr,11)="odbcvartest" Then
objArr(i)="Delete"
strCheck = 2
End If
If Left(oldstr,10)="rpcvartest" Then
objArr(i)="Delete"
strCheck = 2
End If
Next
'If no existing entries --> write entries...
If strCheck=1 Then
WriteNewLine
End If
'Create new array, filtering entries marked "Delete"
ReDim objArrFinal(UBound(objArr))
x = -1
For i = LBound(objArr) to UBound(objArr)
If objArr(i) <> "Delete" Then
x = x + 1
objArrFinal(X) = objArr(i)
End If
Next
'Delete Services
Set objServices = fso.GetFile(strServices)
objServices.Delete
Set objServices = Nothing
'Create new Services
Set objServices = fso.CreateTextFile(strServices, True)
For i = LBound(objArrFinal) to UBound(objArrFinal)
objServices.write objArrFinal(i) & vbNewLine
Next
objServices.close
Set objServices = Nothing