0

This is the very first time i tried batch scripting so please bear with me.

I just wanted to read each line of my hosts file, and replace the line if it contains/matches a substring. I've seen a lot of answered questions about substrings here but I just can't make it work by using the provided solutions.

I have this code:

@echo off
setlocal EnableExtensions EnableDelayedExpansion

set "hostspath=%SystemRoot%\System32\drivers\etc\hosts"
set "hostsbackuppath=c:\hosts"

>"%hostsbackuppath%.new" (
    rem Parse the hosts file, skipping the already present hosts from our list.
    rem Blank lines are preserved using findstr trick.
    for /f "delims=: tokens=1*" %%a in ('%SystemRoot%\System32\findstr.exe /n /r /c:".*" "%hostspath%"') do (

set str1=%%b
    if not x!str1:mydomainname=!==x!str1! (
      rem Match found, replace this line.
      echo "match!"
      set matched=false
    )
    // Didn't match, do not replace
    if not "!matched!"=="true" echo.%%b

    )

)

I was trying out this solution to check for substring match among other else: Batch file: Find if substring is in string (not in a file)

Can someone help me? Thanks

Community
  • 1
  • 1
Bibokid
  • 285
  • 2
  • 9
  • What's the problem then, which part doesn't work? – Bali C Apr 03 '17 at 14:22
  • `if not x%str1:mydomainname=%==x%str1%` should read `if not "!str1:mydomainname=!"=="!str1!"` (supposing you have [delayed expansion](http://ss64.com/nt/delayedexpansion.html) enabled) – aschipfl Apr 03 '17 at 14:22

1 Answers1

0
SETLOCAL ENABLEDELAYEDEXPANSION
set "matched=true"
>"%hostsbackuppath%.new" (
  for /f "delims=: tokens=1*" %%a in ('%SystemRoot%\System32\findstr.exe /n /r /c:".*" "%hostspath%"') do (
    set "str1=%%b"
    if not "!str1:mydomainname=!"=="!str1!" (
      rem Match found, replace this line.
      echo "match at %%b in line %%a"
      set matched=false
    )
    // Didn't match, do not replace
    if not "!matched!"=="true" echo.%%b
  )
)

Hooley-dooley! Someone needs to learn to name variables appropriately.

First, you need to use setlocal enabledelayedexpansion - please see a thousand-and-one SO articles about delayed expansion.

Since str1 is varied within the loop, you need to use setlocal enabledelayedexpansion and !var! to access the varying value of var as %var% is the value at the time the for was encountered.

The syntax SET "var=value" (where value may be empty) is used to ensure that any stray trailing spaces are NOT included in the value assigned. set /a can safely be used "quoteless".

FOr the same reason, quoting each side of a comparison is preferred as it makes a single token of a string containing separators like spaces.

Then you have a comment "match found" after which you set matched to false ?? Therefore you need to initialise match (to true)

Now quite what you want to do is obscure. On re-reading, you probably want to set "matched=true" as the first line within the loop, not outside as I have it, so that the value is re-set to true for each line found and then set to false if a match is found.

All this negative logic is insane. I need a strong cup of coffee.

Magoo
  • 77,302
  • 8
  • 62
  • 84