使用 VBS 脚本在数组中添加和导出字符串
Adding and Exporting strings in array with VBS script
我正在开发一个 VBS 脚本,它会要求用户输入他们想要阻止的网站地址,然后他们输入的内容将被添加到他们计算机的主机文件中,从而使个人将无法访问该特定网站。
换句话说,我想将输入框函数的答案插入到一个数组中,然后将该数组中的字符串导出到另一个文件中。
这是我目前的代码,除了询问输入框给出的两个问题外,它什么都不做——它不会将输入框中的内容写入主机文件。到底出了什么问题,我该如何解决?
非常感谢您的回答
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:\Windows\System32\drivers\etc\hosts")
set oapp = FSO.OpenTextFile("C:\Windows\System32\drivers\etc\hosts", 8, true)
for x = 0 to sites.Count -1
site = sites(x).ToString
oapp.WriteLine ("0.0.0.0" & site)
next
arraylist sites 应该在循环之前初始化,否则总是重置。
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:\Windows\System32\drivers\etc\hosts", 8, true)
For x = 0 to sites.Count -1
site = sites(x)
oapp.WriteLine ("0.0.0.0 " & site)
Next
我正在开发一个 VBS 脚本,它会要求用户输入他们想要阻止的网站地址,然后他们输入的内容将被添加到他们计算机的主机文件中,从而使个人将无法访问该特定网站。
换句话说,我想将输入框函数的答案插入到一个数组中,然后将该数组中的字符串导出到另一个文件中。
这是我目前的代码,除了询问输入框给出的两个问题外,它什么都不做——它不会将输入框中的内容写入主机文件。到底出了什么问题,我该如何解决?
非常感谢您的回答
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:\Windows\System32\drivers\etc\hosts")
set oapp = FSO.OpenTextFile("C:\Windows\System32\drivers\etc\hosts", 8, true)
for x = 0 to sites.Count -1
site = sites(x).ToString
oapp.WriteLine ("0.0.0.0" & site)
next
arraylist sites 应该在循环之前初始化,否则总是重置。
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:\Windows\System32\drivers\etc\hosts", 8, true)
For x = 0 to sites.Count -1
site = sites(x)
oapp.WriteLine ("0.0.0.0 " & site)
Next