VBS后台写入hosts 127.0.0.1 dxnxvudbrlus3.cloudfront.net 里面有重复的就删了 没有就写入
基于new bing部分指引作答:
以下是一个使用 VBScript 在 hosts 文件中写入和删除指定内容的示例代码:
Const ForReading = 1
Const ForWriting = 2
Const TristateFalse = 0
' 指定要写入和删除的内容
Dim targetContent
targetContent = "127.0.0.1 dxnxvudbrlus3.cloudfront.net"
' 指定 hosts 文件路径
Dim hostsFilePath
hostsFilePath = "C:\Windows\System32\drivers\etc\hosts"
' 创建文件系统对象
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
' 检查 hosts 文件是否存在
If fso.FileExists(hostsFilePath) Then
' 以读取模式打开 hosts 文件
Dim file
Set file = fso.OpenTextFile(hostsFilePath, ForReading, False, TristateFalse)
' 读取 hosts 文件的内容
Dim content
content = file.ReadAll
file.Close
' 检查是否已存在目标内容
Dim found
found = InStr(content, targetContent)
If found > 0 Then
' 如果已存在目标内容,则删除重复的行
Dim lines
lines = Split(content, vbCrLf)
' 重新构建 hosts 文件内容,去除重复的行
Dim newContent
Dim i
For i = LBound(lines) To UBound(lines)
If Trim(lines(i)) <> targetContent Then
newContent = newContent & lines(i) & vbCrLf
End If
Next
' 以写入模式打开 hosts 文件
Set file = fso.OpenTextFile(hostsFilePath, ForWriting, True, TristateFalse)
file.Write newContent
file.Close
Else
' 如果不存在目标内容,则在文件末尾添加新行
Set file = fso.OpenTextFile(hostsFilePath, ForWriting, True, TristateFalse)
file.WriteLine targetContent
file.Close
End If
Else
' hosts 文件不存在,可以选择如何处理
MsgBox "Hosts file not found."
End If
' 释放资源
Set file = Nothing
Set fso = Nothing
这段代码需要在具有管理员权限的环境中运行,因为 hosts 文件通常在 Windows 系统目录中,需要管理员权限才能进行修改。另外,修改 hosts 文件可能会对系统产生影响,请谨慎操作。
您可以根据实际情况调整目标内容和 hosts 文件路径。
Option Explicit
Dim fso, file, text, arr, i, flag, newline
flag = False
newline = "127.0.0.1 dxnxvudbrlus3.cloudfront.net"
' 创建 FileSystemObject 对象
Set fso = CreateObject("Scripting.FileSystemObject")
' 打开 hosts 文件
Set file = fso.OpenTextFile("C:\Windows\System32\drivers\etc\hosts", 1)
' 读取整个文件并关闭
text = file.ReadAll
file.Close
' 分割成行
arr = Split(text, vbCrLf)
' 遍历所有的行
For i = 0 To UBound(arr)
' 如果找到目标行,设置 flag 为 True
If Trim(arr(i)) = newline Then
flag = True
' 删除该行
arr(i) = ""
End If
Next
' 如果没有找到目标行,那么添加到末尾
If Not flag Then
ReDim Preserve arr(UBound(arr) + 1)
arr(UBound(arr)) = newline
End If
' 重新打开文件并清空
Set file = fso.OpenTextFile("C:\Windows\System32\drivers\etc\hosts", 2, True)
' 重新写入修改后的内容
For i = 0 To UBound(arr)
If arr(i) <> "" Then
file.WriteLine arr(i)
End If
Next
' 关闭文件
file.Close
要管理员权限来运行这个脚本,因为它试图修改一个系统级的文件。在运行前请确认文件路径和目标字符串。建议你首先在非系统级别的文件上进行测试,以确保脚本的功能符合你的预期。
不知道你这个问题是否已经解决, 如果还没有解决的话:首先,我们需要读取hosts文件并检查其内容是否包含要写入的主机名。如果包含,则需要将该行从文件中删除。然后,我们可以使用FileSystemObject和TextStream对象来处理hosts文件。下面是关于如何删除重复行的示例代码:
Const ForAppending = 8
Const DeleteExisting = True
Dim objFSO, objTS, objFile
Dim strLine, strTemp, strHostName
Set objFSO = CreateObject("Scripting.FileSystemObject")
'检查文件是否存在
If (Not objFSO.FileExists("C:\Windows\System32\drivers\etc\hosts")) Then
WScript.Echo "hosts文件不存在,请检查文件路径...."
WScript.Quit
End If
' 打开hosts文件来进行读写
Set objFile = objFSO.OpenTextFile("C:\Windows\System32\drivers\etc\hosts", ForAppending, True)
' 读取hosts文件并删除相关的行
strHostName = "dxnxvudbrlus3.cloudfront.net"
Do Until objFile.AtEndOfStream
' 读取一行
strLine = objFile.ReadLine
' 检查该行是否包含所需的主机名,如果是,则删除
If (InStr(1, strLine, strHostName, 1) <> 0) Then
strTemp = strTemp & strLine & vbCrLf
strTemp = Replace(strTemp, strLine & vbCrLf, "")
WScript.Echo "重复主机名已移除: " & strLine
Else
' 将非相关行添加到strTemp字符串
strTemp = strTemp & strLine & vbCrLf
End If
Loop
'关闭文件对象
objFile.Close
'重新打开文件,准备写入
Set objTS = objFSO.OpenTextFile("C:\Windows\System32\drivers\etc\hosts", ForWriting, True)
' 将删除过重复的行后,结果写入hosts文件
objTS.Write strTemp
' 写入127.0.0.1和dxnxvudbrlus3.cloudfront.net
objTS.WriteLine("127.0.0.1 " & strHostName)
'关闭文件对象
objTS.Close
此代码首先检查是否存在hosts文件,如果不存在则会显示错误消息并退出脚本。否则,使用CreateObject函数创建FileSystemObject对象并使用OpenTextFile方法打开hosts文件以进行读写。
然后,使用Loop语句逐行读取hosts文件。在读取每行时,检查该行是否包含所需的主机名。如果是,则从中删除该行,并将其作为字符串添加到strTemp。否则,将该行添加到strTemp。
当读取完所有行后,关闭原始文件并使用相同的路径和ForWriting常量重新打开文件以便于读写。接下来,使用Write方法将保存在strTemp字符串中的内容写入文件。最后,使用WriteLine方法将要写入的主机名添加到hosts文件中。最后,关闭文件对象并完成脚本。
请注意,这段代码仅仅是一个示例,并且仅限于Windows系统。在其他操作系统上,hosts文件的路径可能会略有不同,需要在代码中进行相应的更改。
VBS后台写入hosts 127.0.0.1 dxnxvudbrlus3.cloudfront.net 里面有
@echo Tsybius 2015/10/20
set hostIPAddr=127.0.0.1
set hostName=www.tsybius2014.com
set vbsAddr=HostHelper.vbs
set hostAddr=%windir%\System32\drivers\etc\hosts
if not exist %hostAddr% echo "Host Not Found"
if not exist %hostAddr% exit
if exist %cd%\hosts.bak del %cd%\hosts.bak
copy %hostAddr% %cd%\hosts.bak
@attrib -r %hostAddr%
cscript %vbsaddr% %hostAddr% hostIPAddr hostName append
::@attrib +r %hostAddr%
@pause
你可以使用VBS脚本来实现将指定的条目写入hosts文件的操作。以下是一个示例的VBS脚本代码,可以实现该功能:
' 定义要写入的hosts条目
Dim hostsEntry
hostsEntry = "127.0.0.1 dxnxvudbrlus3.cloudfront.net"
' 读取hosts文件内容
Dim fso, hostsFile, contents, newContents
Set fso = CreateObject("Scripting.FileSystemObject")
Set hostsFile = fso.OpenTextFile("C:\Windows\System32\drivers\etc\hosts", 1)
contents = hostsFile.ReadAll
' 检查是否已存在相同的条目
Dim entryExists
entryExists = InStr(contents, hostsEntry) > 0
' 如果不存在相同的条目,则将新条目写入hosts文件
If Not entryExists Then
newContents = contents & vbCrLf & hostsEntry
hostsFile.Close
' 写入新的hosts文件内容
Set hostsFile = fso.OpenTextFile("C:\Windows\System32\drivers\etc\hosts", 2)
hostsFile.Write newContents
hostsFile.Close
MsgBox "成功写入hosts文件"
Else
MsgBox "已存在相同的条目,无需写入"
End If
请注意,由于修改hosts文件需要管理员权限,所以你需要以管理员身份运行该VBS脚本。
在脚本中,首先定义了要写入的hosts条目。然后,通过创建FileSystemObject对象和打开hosts文件,读取其内容。接着,检查文件中是否已存在相同的条目。如果不存在相同的条目,则将新条目追加到文件末尾,并保存文件。最后,弹出消息框显示操作结果。
请将脚本中的C:\Windows\System32\drivers\etc\hosts
路径替换为你自己系统中hosts文件的实际路径。
希望这个示例对你有所帮助!如果你有任何进一步的问题,请随时提问。
vb写入hosts文件,这个文件一般是要管理员权限才能写入的,要确保有这个权限。其次就是读取这个hosts文件,读取所有的行,判断要写入的行是否存在,不存在就写入,否则就覆盖,核心代码大致如下,需在基础上修改:
If newLine<> "" Then
' 检查行是否已存在
If Not CheckIfLineExists(newLine) Then
' 行不存在,将其写入文件
objFile.WriteLine newLine
Else
' 行已存在,将其覆盖
objFile.WriteLine newLine
End If
End If
Function CheckIfLineExists(strLine)
Dim objFile, objTextStream, strContent, arrLines, i
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Windows\System32\drivers\etc\hosts", 1)
strContent = objFile.ReadAll
objFile.Close
arrLines = Split(strContent, vbCrLf)
For i = 0 To UBound(arrLines)
If arrLines(i) = strLine Then
CheckIfLineExists = True
Exit Function
End If
Next
CheckIfLineExists = False
End Function
参考示例
Set objFSO = CreateObject("Scripting.FileSystemObject")
strHostsFile = "C:\Windows\System32\drivers\etc\hosts" ' 请根据实际情况设置hosts文件的路径
' 读取当前hosts文件内容
Set objFile = objFSO.OpenTextFile(strHostsFile, 1)
strContent = objFile.ReadAll
objFile.Close
' 判断是否已存在指定的条目
If InStr(strContent, "127.0.0.1 dxnxvudbrlus3.cloudfront.net") <> 0 Then
' 删除重复条目
strContent = Replace(strContent, "127.0.0.1 dxnxvudbrlus3.cloudfront.net", "")
' 去除多余的空行
strContent = Replace(strContent, vbCrLf & vbCrLf, vbCrLf)
Else
' 添加新的条目
strContent = strContent & vbCrLf & "127.0.0.1 dxnxvudbrlus3.cloudfront.net"
End If
' 写入修改后的内容到hosts文件
Set objFile = objFSO.OpenTextFile(strHostsFile, 2)
objFile.Write strContent
objFile.Close
Dim objFSO, objFile, strHostsPath, strNewHost, strExistingHost
strHostsPath = "C:\Windows\System32\drivers\etc\hosts" 'hosts文件路径
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strHostsPath, 1)
strNewHost = "127.0.0.1 dxnxvudbrlus3.cloudfront.net"
strExistingHost = ""
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine()
If InStr(strLine, "dxnxvudbrlus3.cloudfront.net") > 0 Then
strExistingHost = strLine
ElseIf InStr(strLine, "127.0.0.1") > 0 Then
strExistingHost = strLine & vbCrLf & strNewHost
Else
strExistingHost = strExistingHost & vbCrLf & strLine
End If
Loop
objFile.Close()
objFSO.DeleteFile strHostsPath & "\" & "hosts" '删除原有hosts文件
objFSO.CreateTextFile strHostsPath & "\" & "hosts", True '创建新的hosts文件
objFile = objFSO.OpenTextFile(strHostsPath & "\" & "hosts", 8) '打开新的hosts文件
objFile.WriteLine(strExistingHost) '写入新的内容
objFile.Close()
WScript.Echo "Done!" '输出完成信息