Search code examples
windowsbatch-filecmdtokenize

Read values from tokens in external file in batch script


I have the following file, which defined a version number of a project, which I am using in many places, like CMake files, python scripts, and other configurations. My only struggle is how can I read this in my batch file in windows? The syntax seems a little bit weird and I have to struggle with it for a few hours now and still can't figure it out.

Version.txt:

VERSION_MAJOR 09
VERSION_MINOR 02
VERSION_PATCH 21
VERSION_SUFFIX rc

What I am trying to do, is to use the values of the variables above in my bat script, something like this:

BatchScript.bat

VERSION_MAJOR = @VERSION_MARJO_FROM_FILE (e.g 09)
VERSION_MINOR = @VERSION_MARJO_FROM_FILE (e.g 02)

Solution

  • I found how to do it.

    for /f "tokens=1* delims= " %%i in (version.txt) do (
      if %%i==VERSION_MAJOR call set version_major%%=%%j
      if %%i==VERSION_MINOR call set version_minor%%=%%j
      if %%i==VERSION_PATCH call set version_patch%%
      if %%i==VERSION_SUFFIX call set version_suffix%%
    )