Reputation: 586
I'm doing a for loop in a batch file to set environment variables based on properties from a file, for example like this:
for /F "tokens=1,2 delims==" %%G in (%APPDIR%\config.properties) do (
set %%G=%%H
)
I'd like to do the SET
only if %%G
doesn't start with #
(being a comment), however I can't seem to apply the "substring" technique %var:~0,1%
with %%G
as input. How can I do a if
check based on the first character in %%G
?
I'm guessing I might have missed something obvious (and I'm not quite understanding the %%
-variable concept).
Upvotes: 1
Views: 945
Reputation: 5874
Short answer: you cannot do so directly (according to my knowledge at the point in time where I wrote it; Compo's accepted answer actually disproves this misbelief!)! You will need to pass it to another variable first.
Long answer with the code and more explanation:
By putting the variable value in another variable with the usual percentage signs on each side, you can get the in the way you described in your question.
To make this accessible in the loop though, you will need to use DelayedExpansion which will give you the value "at runtime" in a loop in batch.
To use it, you will need to add the following line above the loop:
setlocal EnableDelayedExpansion
And instead of percentage signs around the variable name use exclamation marks: !varname!
So this should work:
setlocal EnableDelayedExpansion
for /F "usebackq tokens=1,2 delims==" %%G in ("%APPDIR%\config.properties") do (
set helper=%%G
set helper=!helper:~0,1!
REM from here on helper contains the first char from %%G; you can access it with !helper!
REM set %%G=!helper!
)
Further I am unsure if you can or want to actually set %%G
so I used another variable which you need anyways instead. If you want you can include the commented line by removing the REM
Upvotes: 1
Reputation: 38623
If you had read the help or reference information for FOR
you should have seen EOL
, which although it says 'End Of Line' really only refers to one end, the beginning!
For /F "EOL=# UseBackQ Tokens=1* Delims==" %%G In ("%APPDIR%\config.properties"
) Do Set "%%G=%%H"
So the above reads any line not beginning with #
i.e. it ignores your commented lines, which is what you want.
Upvotes: 3
Reputation: 41137
In batch, the string substitution is performed on variables: [SS64]: Variable Edit/Replace.
So, in order to make that work, the content of %%G
has to be assigned to some (temporary) variable and then work with that.
But, since there's a [SS64] FOR /F loop involved, the Delayed Expansion ([SS64] EnableDelayedExpansion; typing set /?
in cmd, will offer information about this topic as well) has to be taken into consideration.
Given all the above, here's your code slightly modified:
@echo off
setlocal enableextensions enabledelayedexpansion
for /F "tokens=1,2 delims==" %%G in (%APPDIR%\config.properties) do (
set _VAR_NAME=%%G
if "!_VAR_NAME:~0,1!" neq "#" (
set !_VAR_NAME!=%%H
)
)
:: Remove/comment the line below, as its only purpose is showcasing the result
set
Upvotes: 2