Calvin
Calvin

Reputation: 377

VBScript delete characters from variable

I have this script

a=inputbox("Wat is het IP adres van de VPS?")
Set oShell = WScript.CreateObject ("WScript.Shell")
oShell.run "cmd.exe /C netsh interface ipv4 set address name=Wi-Fi source=static address="& a &" mask=255.255.254.0 gateway=185.18.148.1 & pause"
msgbox "IP adres: " +a

The user types his VPS IP address into the inputbox. The gateway is VPS IP.1 (for example 8.8.8.1 when VPS IP is 8.8.8.8)

I would like to read the IP variable (a) and delete all characters right from the third dot

Example 1: User types in 176.23.45.102 In this case, 102 should be deleted and the variable should be changed into 176.23.45.

Example 2: User types in 17.23.45.4 In this case, 4 should be deleted and the variable should be changed into 17.23.45.

If not possible in VBScript, (Windows) command line/batch is also good.

Upvotes: 0

Views: 134

Answers (2)

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

Use Split(), zap the 4th element, Join():

>> For Each s In Array("176.23.45.102", "7.23.45.4", "1.2.3.4")
>>     a = Split(s, ".")
>>     a(3) = ""
>>     WScript.Echo s, "=>", Join(a, ".")
>> Next
>>
176.23.45.102 => 176.23.45.
7.23.45.4 => 7.23.45.
1.2.3.4 => 1.2.3.

Inspired by @Ansgar - use a RegExp to zap the offending trailing digit(s):

>> Set r = New RegExp
>> r.Pattern = "\d+$"
>> WScript.Echo """" & r.Replace("1.2.3.4", "") & """"
>>
"1.2.3."

Upvotes: 1

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200293

Another option would be a regular expression:

Set re = New RegExp
re.Pattern = "(.*\.)\d+$"

addr = "176.23.45.102"
WScript.Echo re.Replace(addr, "$1")

Upvotes: 1

Related Questions