Приветствую всех.
Тут такое дело. Имеется батник, который имеет две функции:
1. Создает текстовый файл расширения .ics с добавлением в него нужных строк.
2. Если этот файл со своими строками уже имеется, то батник добавляет в готовый файл только нужные строки не трогая имеющихся и не дублирует те, которые нужно добавить, если таковы имеются
Вот его код:
Скрытый текст
Код:
@echo off
set "File=hosts.ics"
set "Folder=%windir%\system32\drivers\etc"
pushd "%Folder%"||(pause &exit /B 2)
set "@Add001=0.0.0.0 martau.com"
set "@Add002=0.0.0.0 www.martau.com"
set "@Add003=0.0.0.0 total-uninstall.com"
set "@Add004=0.0.0.0 www.total-uninstall.com"
set "@Add005=0.0.0.0 64.91.254.118"
attrib -R -S -H "%File%"
set /A NOld=1000
for /F "usebackq delims=" %%s in ("%File%") do (set /A NOld+=1 &call set "@Old%%NOld%%=%%s")
for /F "usebackq tokens=1* delims==" %%i in (`2^>nul set "@Add"`) do (set /A NOld+=1 &Call set "@Old%%NOld%%=%%j")
for /F "usebackq tokens=1* delims==" %%i in (`2^>nul set "@Old"`) do (set "SS=%%j" &Call set "@%%i=%%SS: =%%")
>"%File%" (for /L %%i in (1001,1,%NOld%) do call :AnalizHosts %%i)
attrib +R "%File%"
popd
exit
:AnalizHosts
call set "A=%%@@Old%1%%"
if /I "%A%"=="" exit /B 1
if "%A:~0,1%"=="#" (call echo %%@Old%1%%&exit /B 0)
(call echo %%@Old%1%%)
for /F "usebackq tokens=1* delims==" %%i in (`2^>nul set "@@Old"`) do if /I "%%j"=="%A%" call set "%%i="
exit /B 0
Сделал точную работу этого батника в NSIS, работает отлично, но код какой-то громоздкой получился.
Можно ли этот код каким нибудь образом упростить или усовершенствовать или он таким и должен быть?
Скрытый текст
Код:
Function DelLine
StrCpy $0 SkipWrite
Push $0
FunctionEnd
Section
Var /global Text
Var /global Text1
Var /global Text2
Var /global Text3
Var /global Text4
StrCpy $Text "0.0.0.0 martau.com"
StrCpy $Text1 "0.0.0.0 www.martau.com"
StrCpy $Text2 "0.0.0.0 total-uninstall.com"
StrCpy $Text3 "0.0.0.0 www.total-uninstall.com"
StrCpy $Text4 "0.0.0.0 64.91.254.118"
${If} ${FileExists} "$SYSDIR\drivers\etc\hosts.ics"
SetFileAttributes "$SYSDIR\drivers\etc\hosts.ics" NORMAL
${LineSum} "$SYSDIR\drivers\etc\hosts.ics" $R0
StrCpy $R1 0
read:
IntOp $R1 $R1 + 1
IntCmp $R1 $R0 0 0 end
readnow:
${LineRead} "$SYSDIR\drivers\etc\hosts.ics" "$R1" $R3
${WordFind} "$R3" "$Text" "E+1{" $R2
IfErrors read 0
${LineFind} "$SYSDIR\drivers\etc\hosts.ics" "" "$R1" "DelLine"
${LineSum} "$SYSDIR\drivers\etc\hosts.ics" $R0
goto readnow
end:
${LineSum} "$SYSDIR\drivers\etc\hosts.ics" $R0
StrCpy $R1 0
read1:
IntOp $R1 $R1 + 1
IntCmp $R1 $R0 0 0 end1
readnow1:
${LineRead} "$SYSDIR\drivers\etc\hosts.ics" "$R1" $R3
${WordFind} "$R3" "$Text1" "E+1{" $R2
IfErrors read1 0
${LineFind} "$SYSDIR\drivers\etc\hosts.ics" "" "$R1" "DelLine"
${LineSum} "$SYSDIR\drivers\etc\hosts.ics" $R0
goto readnow1
end1:
${LineSum} "$SYSDIR\drivers\etc\hosts.ics" $R0
StrCpy $R1 0
read2:
IntOp $R1 $R1 + 1
IntCmp $R1 $R0 0 0 end2
readnow2:
${LineRead} "$SYSDIR\drivers\etc\hosts.ics" "$R1" $R3
${WordFind} "$R3" "$Text2" "E+1{" $R2
IfErrors read2 0
${LineFind} "$SYSDIR\drivers\etc\hosts.ics" "" "$R1" "DelLine"
${LineSum} "$SYSDIR\drivers\etc\hosts.ics" $R0
goto readnow2
end2:
${LineSum} "$SYSDIR\drivers\etc\hosts.ics" $R0
StrCpy $R1 0
read3:
IntOp $R1 $R1 + 1
IntCmp $R1 $R0 0 0 end3
readnow3:
${LineRead} "$SYSDIR\drivers\etc\hosts.ics" "$R1" $R3
${WordFind} "$R3" "$Text3" "E+1{" $R2
IfErrors read3 0
${LineFind} "$SYSDIR\drivers\etc\hosts.ics" "" "$R1" "DelLine"
${LineSum} "$SYSDIR\drivers\etc\hosts.ics" $R0
goto readnow3
end3:
${LineSum} "$SYSDIR\drivers\etc\hosts.ics" $R0
StrCpy $R1 0
read4:
IntOp $R1 $R1 + 1
IntCmp $R1 $R0 0 0 end4
readnow4:
${LineRead} "$SYSDIR\drivers\etc\hosts.ics" "$R1" $R3
${WordFind} "$R3" "$Text4" "E+1{" $R2
IfErrors read4 0
${LineFind} "$SYSDIR\drivers\etc\hosts.ics" "" "$R1" "DelLine"
${LineSum} "$SYSDIR\drivers\etc\hosts.ics" $R0
goto readnow4
end4:
${EndIf}
FileOpen $0 "$SYSDIR\drivers\etc\hosts.log" w
FileWrite $0 "0.0.0.0 martau.com $\r$\n"
FileWrite $0 "0.0.0.0 www.martau.com $\r$\n"
FileWrite $0 "0.0.0.0 total-uninstall.com $\r$\n"
FileWrite $0 "0.0.0.0 www.total-uninstall.com $\r$\n"
FileWrite $0 "0.0.0.0 64.91.254.118 $\r$\n"
FileClose $0
${If} ${FileExists} "$SYSDIR\drivers\etc\hosts.ics"
${FileJoin} "$SYSDIR\drivers\etc\hosts.log" "$SYSDIR\drivers\etc\hosts.ics" "$SYSDIR\drivers\etc\hosts.ics"
Delete "$SYSDIR\drivers\etc\hosts.log"
${Else}
Rename "$SYSDIR\drivers\etc\hosts.log" "$SYSDIR\drivers\etc\hosts.ics"
${EndIf}
SetFileAttributes "$SYSDIR\drivers\etc\hosts.ics" READONLY
SectionEnd