Search code examples
windowsbatch-filecmdini

How to use extracted variables to call a command while iterating through a .ini file in a batch file?


From my initial question with code from @aschipfl. My goal is to iterate through a .ini file, extract the keys and values of an specific section and to use the extracted informations to call a command. The config.ini file, which I want to iterate, looks like this:

[Branches]
trunk=path_to_trunk
k21.1.0.Patch=path_to_k21.1.0.Patch
k21.2.0.Test=path_to_k21.2.0.Test

[Unimportant section]
......

I am currently iterating the config.ini file like suggested by aschipfl in my previous question. Take a look at his answer to see the details.

By the way I would be very pleasured if someone could explain me where the variables, which are storing the informations, get initialized and where I could use them.

Anyways since I now should have the informations I need, even if I don't know exactly where, I want to run a command, based on the informations which were extracted.

The command I want to run is a tortoise command, which is working fine with absolute parameters, but strange error messages appear when I want to pass the variables as parameters:

svn log PATH_TO_BRANCH -v --xml --username xxxxxx --password "xxxxxx" > BRANCH.xml

As you can see above in the config.ini file, the variable PATH_TO_BRANCH is the original value of the variable BRANCH, which in this case would be the key. Note that the variables are named differently in the suggestion of achipfl.

I would be pleasured if someone could help me out here or has any suggestions for me, how I could solve my problem.


Output

Firstly, I split the varibale $%%I after it gets initialized, since $%%I contains the key/value pair. After that I call the command:

for /f "tokens=1*" %%a in ("$%%I") do (
    set "branch=%%a"
    set "path=%%b"
    cmd /c svn log %path% -v --xml --username xxxxxx --password "xxxxxx" > %branch%.xml
)

After running this I get this error:

svn: E205000: Try 'svn help log' for more information svn: 
E205000: 'extensions' option requires 'diff' option

As I said before I don't get this error, if I use plain text instead of variables as parameters. The command svn help log only gives me informations about the usage of the command. The XML file doesn't get created.


Solution

  • As per your example, you broke the systems %path% variable. Never use system variables as your will cause issues in the local session:

    This instance does not require any variable's to be set though as you can use the metavariables:

    @echo off
    for /F "delims=:" %%N in ('findstr /I /N /C:"[items]" "config.ini"') do set "SKIP=%%N"
    for /F "usebackq tokens=1,* skip=%SKIP% delims==" %%I in ("config.ini") do (
        if "%%J" == "" exit /b
        svn log "%%J" -v --xml --username "xxxxxx" --password "xxxxxx" > "%%I.xml"
    )
    

    If you really must set variables, then:

    @echo off
    setlocal enabledelayedexpansion
    for /F "delims=:" %%N in ('findstr /I /N /C:"[items]" "config.ini"') do set "SKIP=%%N"
    for /F "usebackq tokens=1,* skip=%SKIP% delims==" %%I in ("config.ini") do (
        set "branch=%%I"
        set "svnpath=%%J"
        if "%%J" == "" exit /b
        svn log "!svnpath!" -v --xml --username "xxxxxx" --password "xxxxxx" > "!branch!.xml"
    )