Inquiridor
entrar no txt e apagarr somente algumas linhas

Pergunta
-
Todas as Respostas
-
É para apagar as linhas 1 2 3 ou as linhas que seja iguais a, b ou c?
Tente esse, você arrasta o arquivo de texto até o batch e ele apaga as letras a b c
@echo off if not exist "%~1" exit set Texto=%~1 for /f "tokens=1* Delims=]" %%a in ('type "%Texto%" ^|find /i /v /n ""') do Call :Editar "%%~b" del /q "%Texto%" ren "%Texto%.tmp" "%~nx1" exit :Editar set IgualABC= For %%a in (a b c) do IF /i [%~1]==[%%a] set IgualABC=sim IF /i not [%IgualABC%]==[sim]>>"%Texto%.tmp" echo/%~1 goto :EOF
R. Bohner
- Editado Ricardo Bohner quinta-feira, 2 de dezembro de 2021 21:10
-
na verdade desejo apagar algumas linhas dentro do hosts:
set APAGAR="127.0.0.1 youtube.com"
set APAGAR=127.0.0.1 youtube.com.br"
set APAGAR=127.0.0.1 facebook.com.br"
set APAGAR=127.0.0.1 facebook.com"
set APAGAR=127.0.0.1 instagram.com"
set APAGAR=127.0.0.1 instagram.com.br"
set APAGAR=127.0.0.1 pt-br.facebook.com"
set APAGAR=127.0.0.1 www.linkedin.com"
set APAGAR=127.0.0.1 www.linkedin.com.br"
set APAGAR=127.0.0.1 www.twitter.com"
set APAGAR=127.0.0.1 www.twitter.com.br" -
-
Tente esse script:
@echo off SetLocal EnableDelayedExpansion chcp 1252 > Nul net session >nul 2>&1 || (powershell start -verb runas '%~0' &exit /b) set hosts=C:\Windows\System32\drivers\etc\hosts if /i exist "%host%.temp" del /q /f /a "%hosts%.temp" set APAGAR[1]=youtube.com set APAGAR[2]=youtube.com.br set APAGAR[3]=facebook.com.br set APAGAR[4]=facebook.com set APAGAR[5]=instagram.com set APAGAR[6]=instagram.com.br set APAGAR[7]=pt-br.facebook.com set APAGAR[8]=www.linkedin.com set APAGAR[9]=www.linkedin.com.br set APAGAR[10]=www.twitter.com set APAGAR[11]=www.twitter.com.br for /f "delims=" %%a in ('type "%hosts%"') do ( set Linha=%%a IF "!Linha:~0,1!"=="#" set /a NComentarios+=1 ) set /a LinhaEmBranco=%NComentarios%+1 for /f "Delims=" %%a in ('type "%hosts%"') do call :Desbloquear "%%~a" del /q /f "%hosts%" ren "%hosts%.temp" hosts exit :Desbloquear set /a NLinhas+=1 set "Linha=%~1" IF %NLinhas% EQU %LinhaEmBranco% echo.>>"%hosts%.temp" IF "%Linha:~0,1%"=="#" >>"%hosts%.temp" echo %~1& goto :EOF for /L %%a in (1,1,11) do ( echo %~1 | find /i "!APAGAR[%%a]!" 1>nul 2>nul IF !Errorlevel! EQU 0 goto :EOF ) >>"%hosts%.temp" echo %~1 goto :EOF
R. Bohner
-
-
-
Boa tarde.
É muito mais fácil fazer isso em um console Linux. Imagino que seu sistema já tem WSL. Em vez de usar um arquivo batch, use um shell script:-----------------------------------------------------------------------
#!/bin/bash
#
echo "Apagando linhas strings youtube, facebook, instagram e linkedin, no arquivo hosts ..." ;
sed -i '/youtube\|facebook\|instagram\|linkedin/d' C:/System32/drivers/etc/hosts && echo "OK." ;
exit
-----------------------------------------------------------------------
Copie o script acima e salve como hosts.sh. Abra um terminal Linux, execute "chmod +x hosts.sh" (para tornar o script executavel) e depois rode o script com o comando "./hosts.sh"
-
-
Esse parte deveria apagar o hosts e renomear o %hosts%.temp para hosts:
del /q /f "%hosts%" ren "%hosts%.temp" hosts exit
Tente colocar um pause antees do exit para ver qual erro aparece. Também verifica seo a variável %hosts% realmente aponta para o arquivo hosts
R. Bohner
-
-
-
-
-
-
O usuário que você usa para rodar o bat é um administrador? Verifique as permissões novamente em Propriedades -> Segurança e tente desabilitar o antivírus pode ser que ele esteja bloqueando o bat.
Manualmente sem ser por bat você consegue fazer modificações no hosts?
R. Bohner
-
-