1

I found the code for my loop in this answer. Now I modified to loop a bit to match my needs and now it looks like this:

for /f "delims=" %%x in (test-config.txt) do (
    call set "%%x"

echo %%x |findstr /b "SERVICE_NAME" >nul ||(

call set "def=%%def%% --"%%x""  
) || (
 echo 

)
)

the loop reads a file with following format:

par1=val1
par2=val2
par3=val3

and its working as needed. Now I want to add some comments in between the lines like this:

par1=val1
par2=val2
//value is not default
par3=val3

sadly the loop now tries to create a parameter with the comment as name and no value. Can you show me how I have to enhance my loop to ignore the comment lines? Please note that a value could also include //

XtremeBaumer
  • 6,275
  • 3
  • 19
  • 65
  • Include the `=` sign in the search string of `findstr`, and implement another filter for `//`: `echo(%%x| > nul 2>&1 (findstr /V /B /C:"//" | findstr /B /C:"SERVICE_NAME=") || ( ... )` (remove the `2>&1` part to not suppress error messages) – aschipfl Aug 08 '17 at 11:42
  • @aschipfl thanks for your answer. i replaced `echo %%x |findstr /b "SERVICE_NAME" >nul ||` with your `echo(%%x| > nul 2>&1 (findstr /V /B /C:"//" | findstr /B /C:"SERVICE_NAME=") ||` but i still get the error `Environment variable// value is not default not defined` – XtremeBaumer Aug 08 '17 at 11:47
  • 3
    `for /f "eol=/ delims=" %%x`? – MC ND Aug 08 '17 at 11:50
  • @MCND could you please explain the code? i am not very common with batch scripts – XtremeBaumer Aug 08 '17 at 11:52
  • 2
    Sorry, the `eol` clause in the `for` command defines a character that will be used as a *discard flag*. Lines starting with this character (after delimiter removal) are discarded – MC ND Aug 08 '17 at 11:56
  • @MCND ahh that sounds very good. thanks for this solution. if you want, write it as answer and i'll accept it – XtremeBaumer Aug 08 '17 at 12:00
  • 1
    One weakness of the `eol=/` clause is that it will also flag a line starting with a single slash as a comment, and it is limited to a single character so that can't be overriden easily. Probably not an issue in this case, but you should be aware. – Steven K. Mariner Aug 08 '17 at 16:29
  • @StevenK.Mariner thanks for your information. you are right that it shouldn't be an issue since i don't plan on adding parameter starting with a slash – XtremeBaumer Aug 11 '17 at 06:14

1 Answers1

0

As mentioned by MC ND,

for /f "eol=/ delims=" %%x works just fine.

Explanation:

The eol clause in the for command defines a character that will be used as a discard flag. Lines starting with this character (after delimiter removal) are discarded

XtremeBaumer
  • 6,275
  • 3
  • 19
  • 65