Batch file 创建BAT文件以从宿主文件中删除或删除特定行
我是新来的。我需要帮助创建一个BAT文件,以便从主文件中删除特定行。对不起,我不是一个程序员,但不知怎么的,我能理解。我从不同的网站下载了一个脚本,并做了一些轻微的修改和编译,以满足我的特殊需要。对于已编译脚本的概述,它将执行以下操作:Batch file 创建BAT文件以从宿主文件中删除或删除特定行,batch-file,Batch File,我是新来的。我需要帮助创建一个BAT文件,以便从主文件中删除特定行。对不起,我不是一个程序员,但不知怎么的,我能理解。我从不同的网站下载了一个脚本,并做了一些轻微的修改和编译,以满足我的特殊需要。对于已编译脚本的概述,它将执行以下操作: 首先打开脚本时,它会询问您的姓名。稍后我将使用它在主机文件中记录我的活动 它会问你想做什么,有三种选择。添加、删除和退出 当您输入addnew时,它会要求您在IP旁边输入域名 当您继续操作时,它将完成它的工作,在主机文件中插入新行刷新DNS并在文本文件中复制 当
@echo off
echo.
title Host File Editor
:start
cls
echo.
echo What do you want to do?
echo.
echo 1. Add New Entry
echo 2. Delete Entry
echo 3. Exit
echo.
set /p where=
echo.
if %where% equ 1 goto add_new
if %where% equ 2 goto del_entry
if %where% equ 3 goto rejected
cls
:add_new
cls
echo.
echo Enter Domain Name
echo.
set /p domain=Domain Name:
cls
echo.
echo Enter Domain IP Address
echo.
set /p ip=IP Address:
echo New entry (%ip% %domain%) will be added to Host File
echo.
pause
cls
goto :accepted
:--------------------------------------------
:REJECTED
cls
ECHO Your HOSTS file was left unchanged.
ECHO Finished.
echo.
pause
GOTO EOF
:ACCEPTED
setlocal enabledelayedexpansion
::Create your list of host domains
set LIST=(%domain%)
::Set the ip of the domains you set in the list above
set %domain%=%ip%
:: deletes the parentheses from LIST
set _list=%LIST:~1,-1%
::ECHO %WINDIR%\System32\drivers\etc\hosts > host.txt
for %%G in (%_list%) do (
set _name=%%G
set _value=!%%G!
SET NEWLINE=^& echo.
ECHO Carrying out requested modifications to your HOSTS file
::strip out this specific line and store in tmp file
type %WINDIR%\System32\drivers\etc\hosts | findstr /v !_name! > host.txt
::re-add the line to it
ECHO %NEWLINE%^!_value! !_name!>>host.txt
::overwrite host file
copy /b/v/y tmp.txt %WINDIR%\System32\drivers\etc\hosts
)
ipconfig /flushdns
ECHO.
pause
ECHO New entry successfully added!
ECHO.
goto :start
;-------Delete Entry--------------------------------------
:del_entry
cls
echo.
echo Enter Domain Name
echo.
set /p domain=Domain Name:
cls
echo.
echo Enter Domain IP Address
echo.
set /p ip=IP Address:
echo %ip% %domain% will be removed from Host File
echo %ip% %domain%>ip.txt
echo.
pause
cls
@echo off
FOR /F "delims=^n tokens=*" %%a IN (%ip% %domain%) do call :func "%%a"
:func
set line=%1
set hf=%windir%\system32\drivers\etc\hosts
find /v %line% < %hf% >> newhosts
rename newhosts hosts
copy /a /y hosts %hf%
pause
goto start
@echo关闭
回声。
标题宿主文件编辑器
:开始
cls
回声。
你想做什么?
回声。
回声1。添加新条目
回声2。删除条目
回声3。出口
回声。
设置/p在哪里=
回声。
如果%where%eq 1转到添加新
如果%where%eq 2转到del_条目
如果%where%eq 3转到被拒绝
cls
:新增
cls
回声。
输入域名
回声。
set/p domain=域名:
cls
回声。
输入域IP地址
回声。
设置/p ip=ip地址:
回显新条目(%ip%%域%)将添加到主机文件
回声。
暂停
cls
后藤:接受
:--------------------------------------------
:拒绝
cls
ECHO主机文件保持不变。
回声结束了。
回声。
暂停
转到EOF
:接受
延迟扩展
::创建主机域列表
设置列表=(%domain%)
::设置您在上面列表中设置的域的ip
设置%domain%=%ip%
::从列表中删除括号
集合_list=%list:~1,-1%
::ECHO%WINDIR%\System32\drivers\etc\hosts>host.txt
对于%%G in(%u列表%),请执行以下操作(
集合_name=%%G
设置_值=!%%G!
设置换行符=^&回显。
ECHO对主机文件执行请求的修改
::去掉此特定行并存储在tmp文件中
键入%WINDIR%\System32\drivers\etc\hosts | findstr/v!_name!>host.txt
::将该行重新添加到其中
回显%NEWLINE%^!\u值!!\u名称!>>host.txt
::覆盖主机文件
复制/b/v/y tmp.txt%WINDIR%\System32\drivers\etc\hosts
)
ipconfig/flushdns
回声。
暂停
已成功添加新条目!
回声。
后藤:开始
;-------删除条目--------------------------------------
:del_条目
cls
回声。
输入域名
回声。
set/p domain=域名:
cls
回声。
输入域IP地址
回声。
设置/p ip=ip地址:
回显%ip%%域%将从主机文件中删除
echo%ip%%domain%>ip.txt
回声。
暂停
cls
@回音
对于/F“delims=^n令牌=*”(%ip%%域%)中的%%a,请调用:func“%%a”
:func
设置行=%1
设置hf=%windir%\system32\drivers\etc\hosts
查找/v%line%<%hf%>>新主机
重命名新主机
复制/a/y主机%hf%
暂停
开始
我真的很感激你的帮助。提前感谢。此脚本的编码确实不好,应该完全重写。我将通过删除第二行上已经存在的第一个更改
start
是cmd.exe
的内部命令。因此,此字符串可以,但不应用作标签<代码>选项比设置/p更适合菜单<代码>其中是一个外部命令(可在目录%SystemRoot%\System32
中执行)。可以使用名为的环境变量,其中
,但不应这样做。批处理文件未考虑在内,因此在32位环境中的64位Windows上执行批处理文件时完全失败,因为在64位Windows上只有%SystemRoot%\System32\drivers\etc\hosts,
,但根本没有%SystemRoot%\SysWOW64\drivers\etc
。您好,感谢您的快速响应。我会再试一次。谢谢