Trim in loop
Hi,
I have c:\test\iplist.txt.
This contains lists of IP Addresses (I've put XXX instead of the numbers):
(IP)-10.XXX.XX.XXX
(IP)-10.XXX.XX.XX
(IP)-10.XXX.XX.XXX
(IP)-10.XXX.XX.XXX
(IP)-10.XXX.XX.XXX
(IP)-10.XXX.XX.XXX
(IP)-10.XXX.XX.XXX
What's the best way to go through the list and trim the first 5 characters from each line?
Thanks.
I have c:\test\iplist.txt.
This contains lists of IP Addresses (I've put XXX instead of the numbers):
What's the best way to go through the list and trim the first 5 characters from each line?
Thanks.
0 Comments
[ + ] Show comments
Answers (3)
Please log in to answer
Posted by:
anonymous_9363
16 years ago
- Use the FileSystemObject to open the file
- Use the .ReadAll method to read the file's contents into a string
- Use Split to convert the string into an array, using VBS's built-in vbNewLine constant as the separator for Split
- Do a normal 'For x = LBound(arrTextFile) To UBound(arrTextFile)' to get each element's value
- Use Split again to build the string you require, this time using '.' as the separator. I suggest that rather than Left, Right or Mid because you'll presumably be getting addresses like '1.1.1.1' as well as '255.255.255.255' i.e. you can't easily tell what 5 characters you want unless you add reams of code to check the length of each address segment.
- Use the .ReadAll method to read the file's contents into a string
- Use Split to convert the string into an array, using VBS's built-in vbNewLine constant as the separator for Split
- Do a normal 'For x = LBound(arrTextFile) To UBound(arrTextFile)' to get each element's value
- Use Split again to build the string you require, this time using '.' as the separator. I suggest that rather than Left, Right or Mid because you'll presumably be getting addresses like '1.1.1.1' as well as '255.255.255.255' i.e. you can't easily tell what 5 characters you want unless you add reams of code to check the length of each address segment.
Posted by:
gmorgan618
16 years ago
This will msgbox each entry with the first 5 characters removed.
' vbCrLf is also a vbs constant - that handles the 'Returns' at the end of each line.
'Setup File system object - of course - my referene to this is oFSO
Dim fileData : fileData = oFSO.ReadAll
Dim arrIPData : arrIPData = Split(fileData, vbCrLf)
Dim strIP '<------------- EDIT ------ OOps forgot to dim this
For Each strIP in arrIPData
Dim StripedIP : StripedIP = ""
If len(strIP) > 5 Then StripedIP = Right(strIP,(len(Trim(strIP)) - 5))
msgbox(StripedIP)
Next
-------------------
If you need to deal with an unknown ip such as 1.1.1.1 to 255.255.255.255 as VBScab mentions then do this..
For Each strIP in arrIPData
Dim arrIP : arrIP = Split(strIP, ".")
'The desired strip from a full IP -- xxx.xxx.xxx.xxx
Dim intCharToStrip : intCharToStrip = 6
'Determine if first octet is less than 3 numbers and subtract from number to strip
'since you have an ip of 10.xxx.xxx.xxx - this will set your number to 5
intCharToStrip = intCharToStrip - (3 - len(arrIP(0)))
'Same concept, but will detect if the second octet is not full...
intCharToStrip = intCharToStrip - (3 - len(arrIP(1)))
Dim StripedIP : StripedIP = ""
If len(strIP) > intCharToStrip Then StripedIP = Right(strIP,(len(Trim(strIP)) - intCharToStrip ))
msgbox(StripedIP)
Next
' vbCrLf is also a vbs constant - that handles the 'Returns' at the end of each line.
'Setup File system object - of course - my referene to this is oFSO
Dim fileData : fileData = oFSO.ReadAll
Dim arrIPData : arrIPData = Split(fileData, vbCrLf)
Dim strIP '<------------- EDIT ------ OOps forgot to dim this
For Each strIP in arrIPData
Dim StripedIP : StripedIP = ""
If len(strIP) > 5 Then StripedIP = Right(strIP,(len(Trim(strIP)) - 5))
msgbox(StripedIP)
Next
-------------------
If you need to deal with an unknown ip such as 1.1.1.1 to 255.255.255.255 as VBScab mentions then do this..
For Each strIP in arrIPData
Dim arrIP : arrIP = Split(strIP, ".")
'The desired strip from a full IP -- xxx.xxx.xxx.xxx
Dim intCharToStrip : intCharToStrip = 6
'Determine if first octet is less than 3 numbers and subtract from number to strip
'since you have an ip of 10.xxx.xxx.xxx - this will set your number to 5
intCharToStrip = intCharToStrip - (3 - len(arrIP(0)))
'Same concept, but will detect if the second octet is not full...
intCharToStrip = intCharToStrip - (3 - len(arrIP(1)))
Dim StripedIP : StripedIP = ""
If len(strIP) > intCharToStrip Then StripedIP = Right(strIP,(len(Trim(strIP)) - intCharToStrip ))
msgbox(StripedIP)
Next
Posted by:
Meic
16 years ago
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.