Показать полную графическую версию : Проверка файла host
Уважаемые форумчане, подсобите, пожалуйста,
Я хочу, чтобы при запуске CMD-файла проверялся файл host на наличие строчек:
127.0.0.1 rad.msn.com
127.0.0.1 apps.skype.com
127.0.0.1 download.skype.com
127.0.0.1 g.msn.com
127.0.0.1 cdn.eyewonder.com
Если строчки присутствуют, то CMD-файл исполняется дальше, если строчек нет, они добавляются и CMD-файл исполняется дальше.
Подскажите, как это реализовать???
Для полной замены host я использую скрипт:
@echo off
@echo # Copyright (c) 1993-2009 Microsoft Corp. > %windir%\system32\drivers\etc\hosts
@echo # >> %windir%\system32\drivers\etc\hosts
@echo # This is a sample HOSTS file used by Microsoft TCP/IP for Windows. >> %windir%\system32\drivers\etc\hosts
@echo # >> %windir%\system32\drivers\etc\hosts
@echo # This file contains the mappings of IP addresses to host names. Each >> %windir%\system32\drivers\etc\hosts
@echo # entry should be kept on an individual line. The IP address should >> %windir%\system32\drivers\etc\hosts
@echo # be placed in the first column followed by the corresponding host name. >> %windir%\system32\drivers\etc\hosts
@echo # The IP address and the host name should be separated by at least one >> %windir%\system32\drivers\etc\hosts
@echo # space. >> %windir%\system32\drivers\etc\hosts
@echo # >> %windir%\system32\drivers\etc\hosts
@echo # Additionally, comments (such as these) may be inserted on individual >> %windir%\system32\drivers\etc\hosts
@echo # lines or following the machine name denoted by a '#' symbol. >> %windir%\system32\drivers\etc\hosts
@echo # >> %windir%\system32\drivers\etc\hosts
@echo # For example: >> %windir%\system32\drivers\etc\hosts
@echo # >> %windir%\system32\drivers\etc\hosts
@echo # 102.54.94.97 rhino.acme.com # source server >> %windir%\system32\drivers\etc\hosts
@echo # 38.25.63.10 x.acme.com # x client host >> %windir%\system32\drivers\etc\hosts
@echo. >> %windir%\system32\drivers\etc\hosts
@echo # localhost name resolution is handled within DNS itself. >> %windir%\system32\drivers\etc\hosts
@echo # 127.0.0.1 localhost >> %windir%\system32\drivers\etc\hosts
@echo # ::1 localhost >> %windir%\system32\drivers\etc\hosts
@echo. >> %windir%\system32\drivers\etc\hosts
@echo 127.0.0.1 download.skype.com >> %windir%\system32\drivers\etc\hosts
ipconfig /flushdns
pause
Но мне надо, чтобы проверялось наличие конкретных строчек.
@echo off
setlocal enableextensions enabledelayedexpansion
set sSourceFile=C:\Песочница\069\list.txt
set sDestFile=%SystemRoot%\system32\drivers\etc\hosts
if exist "%sSourceFile%" (
if exist "%sDestFile%" (
for /f "usebackq delims=" %%i in ("%sSourceFile%") do (
type "%sDestFile%" | >nul 2>&1 find.exe /i "%%~i" || (
echo Add string [%%~i].
>>"%sDestFile%" echo %%~i
)
)
) else (
echo Can't find destination file [%sDestFile%].
exit /b 2
)
) else (
echo Can't find source file [%sSourceFile%].
exit /b 1
)
endlocal
exit /b 0
А как сделать без дополнительного файла list.txt???
Чтобы строчки были прописаны в cmd.
Sisin, по-хорошему, конечно, вместо «find.exe» надо пользовать «findstr.exe» с указанием в маске начала и конца строки, наподобие:
type "%sDestFile%" | >nul 2>&1 findstr.exe /i /r /c:"^%%~i$" || (
а то попадётся в исходном файле что-нибудь этакое:
127.0.0.1 g.msn.com.ru
127.0.0.1 g.msn.com
— и будет ошибочно определено, что вторая строка уже есть в целевом файле (по совпадению подстроки).
А как сделать без дополнительного файла list.txt???
Чтобы строчки были прописаны в cmd. »
А чем оно Вам мешает — отдельным файлом? Возни чуть больше.
А чем оно Вам мешает — отдельным файлом? Возни чуть больше. »
а как сделать относительный путь до list.txt???
а как сделать относительный путь до list.txt??? »
Относительно самого пакетного файла? Например, так:
set sSourceFile=%~dp0list.txt
— в том же каталоге, что и пакетный файл.
У меня после запуска дублируются строчки.
т.е. 127.0.0.1 download.skype.com уже была, а после запуска таких строчек стало две.
Подумал, может пробелы в конце мешают, но нет не из-за них...
У меня после запуска дублируются строчки. »
Упакуйте в архив Ваш файл «host» и файл «list.txt». Выложите архив сюда или на RGhost.
Я разобрался, они были в разных кодировках.
Спасибо, Iska.
© OSzone.net 2001-2012
vBulletin v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.