1

I have a property file with name 'props.txt' which has values like -

test=props
#test2=props

I have written a batch script to put these properties in Windows 10 environment variables as following:

#@ECHO OFF
For /F "tokens=1* delims==" %%A IN (props.txt) DO (

    IF NOT "%A:~0,1%"=="#" (
        SETX "%%A"  "%%B"
    )
)

Now all the properties are put into environment variables, even if they start with '#'. I want to ignore the properties which start with '#'. How can I do that? Also, I want to skip blank lines. Is there any change which I would need to do?

Popeye
  • 1,548
  • 3
  • 25
  • 39
  • You cannot do string manipulation on a `FOR` token variable. You have to assign it to an environmental variable first. – Squashman Sep 17 '18 at 13:35

1 Answers1

3

You could use EOL=# it skips all lines beginning with #, the default is EOL=;.
Blank lines are always skipped by FOR /F, it's a problem to fetch them, if you want to.

For /F "tokens=1* EOL=# delims==" %%A IN (props.txt) DO (
    ...
jeb
  • 78,592
  • 17
  • 171
  • 225
  • Can you also tell me if there is any simple way to trim keys and values before putting them to environment variables? – Popeye Sep 17 '18 at 12:58
  • 1
    @N.. Take a look at [Remove trailing spaces from a file using Windows batch?](https://stackoverflow.com/a/9318485/463115) That also works for variables – jeb Sep 18 '18 at 06:03