/build/static/layout/Breadcrumb_cap_w.png

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.

0 Comments   [ + ] Show comments

Answers (3)

Posted by: anonymous_9363 16 years ago
Red Belt
0
- 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.
Posted by: gmorgan618 16 years ago
Blue Belt
0
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
Posted by: Meic 15 years ago
Second Degree Blue Belt
0
Thanks to you both for your excellent replies. Sorry I hadn't acknolwledge you replies any sooner - I must have had to move onto other tasks at the time.
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ