Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy Array to Array deleting entries 1

Status
Not open for further replies.

JadeKnight

IS-IT--Management
Feb 23, 2004
94
NO
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

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
 
why dont you just loop through the array whcih contains the "delete" string and only write to the new file if the array element <> "delete"?
 
That's a solution wich will work :)... When I started writing it, there were a couple of thoughts/ideas wich were rejected, but not the design. Sometimes you get blinded ;)...

However, I'm pretty curious about the solution. If anyone know the answear, it will still be appreciated.
 
its because you dim your second array as follows:

ReDim objArrFinal(UBound(objArr))

so you start off by making it the same size as your first array including the "delete" entries.

you then loop through and dont add the delete entries but your second array is still dim'd with n X empty elemets at the end.

what you need to do is Redim Preserve your array as you populate it

so,

Dim objArrFinal()
For i = LBound(objArr) to UBound(objArr)
If objArr(i) <> "Delete" Then
x = x + 1
ReDim Preserve objArrFinal(X)
objArrFinal(X) = objArr(i)
End If
Next


might do it
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top