Text replacing Batch File

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
davsank
Posts: 3
Joined: 31 Mar 2015 03:55

Text replacing Batch File

#1 Post by davsank » 31 Mar 2015 04:01

I build a batch script that essentially looks for a ceratin entry in the hosts file and disables it by adding # before said value.
it works by renaming the original file, then copying the information from the renamed file to a new file that names as the original.
However, for some reason, it skips after a space bar and drop the rest of the line....

the code is :

Code: Select all

@echo off
echo searching for domain redirection in hosts file, Please wait.
find "my.domain.com" c:\test\hosts >nul
if %errorlevel% equ 1 goto notfound
goto found

:found
color c
echo found domain redirection - deleting
        SETLOCAL=ENABLEDELAYEDEXPANSION

        rename c:\test\hosts hosts.old
        for /f %%a in (c:\test\hosts.old) do (
            set foo=%%a
            if !foo!==my.domain.com set foo=#
            echo !foo! >> c:\test\hosts)
    del c:\test\hosts.old
pause>nul

:notfound
color a
echo hosts file does not contain redirection.



pause>nul


Any ideas?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Text replacing Batch File

#2 Post by foxidrive » 31 Mar 2015 04:21

This can be a one liner:

Code: Select all

@echo off
jrepl "my.domain.com" "#my.domain.com" /L /I /F "c:\test\hosts" /O -



This uses a native Windows batch script called Jrepl.bat (by dbenham)
- download from: https://www.dropbox.com/s/4otci4d4s8x5ni4/Jrepl.bat

Place it in the same folder as the batch file, or in a folder that is on the system path.

Windows now blocks many downloaded files to protect the user
and it can be unblocked by this step:

* Right click the batch file
* select the "Properties" entry
* and click "Unblock"

davsank
Posts: 3
Joined: 31 Mar 2015 03:55

Re: Text replacing Batch File

#3 Post by davsank » 31 Mar 2015 04:29

thanks for the suggestion,
however, I need a solution that does not involve any external scripts - a simple run and forget file without any other additions...

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Text replacing Batch File

#4 Post by foxidrive » 31 Mar 2015 04:34

davsank wrote:thanks for the suggestion,
however, I need a solution that does not involve any external scripts - a simple run and forget file without any other additions...


It's as external as the script is that you are writing yourself,
and has additional benefits also - it's robust and reliable.

You can find it on this forum too.

davsank
Posts: 3
Joined: 31 Mar 2015 03:55

Re: Text replacing Batch File

#5 Post by davsank » 31 Mar 2015 07:17

foxidrive wrote:
davsank wrote:thanks for the suggestion,
however, I need a solution that does not involve any external scripts - a simple run and forget file without any other additions...


It's as external as the script is that you are writing yourself,
and has additional benefits also - it's robust and reliable.

You can find it on this forum too.


True, but I needed something that can run as part of a logon script.
At any rate.. I solved it myself:

Code: Select all

@echo off
setlocal enabledelayedexpansion
set HostsFile=C:\test\hosts
set Domain=my.domain.com
type "%HostsFile%" | findstr.exe /b /i "%Domain%" >NUL
if errorlevel 1 (
   color a
   echo hosts file does not contain redirection for '%Domain%'.
   pause
   color
   exit /b
)
color c
echo found domain redirection for '%Domain%' - deleting ...
for %%a in ("%HostsFile%") do (
   set BackupFile=%%~fa.old
   rename "%%~a" "%%~nxa.old"
)
for /f "tokens=1* delims=[]" %%a in ('type "%BackupFile%" ^| find.exe /n /v ""') do (
   set Line=%%b
   echo.!Line!| findstr.exe /b /i "%Domain%" >NUL
   if not errorlevel 1 set Line=# !Line!
   echo.!Line!
   >>"%HostsFile%" echo.!Line!
)
pause
del "%BackupFile%"
color
exit /b

Post Reply