创建BAT文件以从主机文件中删除或删除特定行

时间:2018-08-09 07:36:23

标签: batch-file

我是新来的。我需要帮助来创建BAT文件,而不是可以从主机文件中删除特定行。抱歉,我不是编码人员,但可以理解。我已经从其他站点下载了脚本,并进行了一些修改,并进行了编译以满足我的特定需求。对于我的编译脚本的概述,它将执行以下操作:

  1. 首先打开脚本后,它将询问您的名字。稍后我将使用它来将活动记录在主机文件中。

  2. 它将询问您您想做什么,共有三种选择。添加新的,删除并退出。

  3. 当您输入添加新内容时,它将要求您在IP旁边输入一个域名 然后继续操作,它将完成工作,在主机文件中插入新行以刷新DNS并在文本文件中进行复制。

  4. 当我选择删除行时,过程仍然相同,唯一的区别是它收到错误消息“查找参数格式不正确”。我不知道。

这是我的代码:

@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

几乎没有什么帮助,不胜感激。预先感谢。

0 个答案:

没有答案