使用VBS脚本添加和导出数组中的字符串



我正在编写一个VBS脚本,该脚本将要求用户输入他们想要阻止的网站的地址,然后他们输入的地址将被添加到他们计算机的hostsfile中,从而使个人无法访问该特定网站。

换句话说,我想将inputbox函数的答案插入到一个数组中,然后将该数组中的字符串导出到另一个文件中。

这是我的代码,它没有做任何事情,只是问输入框框给出的两个问题——它没有把输入到框中的内容写到hostsfile中。到底是什么错了,我怎么能解决它?

非常感谢您的回答

dim result
dim sites
x = 0
Do
Set sites = CreateObject("System.Collections.ArrayList")
result = Inputbox("What site do you wanted blocked? Please include entire address.") 
result2 = MsgBox("Would you like to add another site at this time?", vbQuestion + vbYesNo)
If result2 = vbNo Then
Exit Do
End If
sites.add result
Loop
Set FSO = CreateObject("Scripting.FileSystemObject")
Set Hosts = FSO.GetFile("C:WindowsSystem32driversetchosts")
set oapp = FSO.OpenTextFile("C:WindowsSystem32driversetchosts", 8, true)
for x = 0 to sites.Count -1
site = sites(x).ToString
oapp.WriteLine ("0.0.0.0" & site)
next

数组列表sites应该在循环之前初始化,否则它总是被重置。

网站。add应放在Exit Do之前,否则将不包含最后的结果。

Dim result
Dim sites
Set sites = CreateObject("System.Collections.ArrayList")
Do
result = Inputbox("What site do you wanted blocked? Please include entire address.") 
sites.add result
result2 = MsgBox("Would you like to add another site at this time?", vbQuestion + vbYesNo)
If result2 = vbNo Then
Exit Do
End If
Loop
Set FSO = CreateObject("Scripting.FileSystemObject")
Set oapp = FSO.OpenTextFile("C:WindowsSystem32driversetchosts", 8, true)
For x = 0 to sites.Count -1
site = sites(x)
oapp.WriteLine ("0.0.0.0 " & site)
Next

最新更新