batch file skips spaces

I'm trying to make a batch file to disable a certain redirection in the hosts file. I started out with this mock hosts file:
# Copyright (c) 1993-2006 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handle within DNS itself.
#       127.0.0.1       localhost
#       ::1             localhost
banana a.b.c.d
my.domain.com 1.2.3.4
funny 12.13.14.15

and then used the script I built:
@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=#my.domain.com
            echo !foo! >> c:\test\hosts) 
    del c:\test\hosts.old
pause>nul
exit 

:notfound
color a
echo hosts file does not contain redirection.
pause>nul
exit

Open in new window


which eventually left me with this:
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
banana
#my.domain.com
funny

The copying mechanism stopped whenever it reached a space and moved on to the next line, which could be a big problem if anyone has any other active redirections.

Any ideas?
LVL 8
David SankovskySenior SysAdminAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Try this:
@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

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JohnBusiness Consultant (Owner)Commented:
In addition to the above, you need to run this script in administrative mode or it won't save, del, or rename the hosts file. I cannot see that you are working as administrator in the script above.
0
David SankovskySenior SysAdminAuthor Commented:
Worked Like Magic!!
Thanks a lot!
0
David SankovskySenior SysAdminAuthor Commented:
Worked perfectly on the first run!
Easy to reconfigure if needed, just what I needed! A+!
0
Steve KnightIT ConsultancyCommented:
Saw this earlier but couldn't respond.  Was just going to suggest using

find /v "domain.to.remove.com" <hosts.old > hosts

to remove that line altogether?

Steve
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.