【问题标题】:Adding and Exporting strings in array with VBS script使用 VBS 脚本在数组中添加和导出字符串
【发布时间】:2021-02-19 21:05:29
【问题描述】:

我正在编写一个 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

【问题讨论】:

    标签: arrays string vbscript


    【解决方案1】:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-20
      • 2012-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多