rickg_la
rickg_la

Reputation: 13

Parse an environment variable in a Windows BAT script

I'm trying to create a simple application launcher for the software Nuke that puts the path together by evaluating an environment variable. The value of the variable is used in two ways. First it's used as-is. The second time I need to split the variable and use the first half.

The env variable set for the system:

NUKE_VERSION = 10.0v5

The path to the application:

C:\Program Files\Nuke10.0v5\Nuke10.0.exe

The code below works fine on the cmd prompt:

FOR /F "delims=v tokens=1" %i IN ("%NUKE_VERSION%") DO set NUKE_MAJOR=%i
"C:\Program Files\Nuke%NUKE_VERSION%\Nuke%NUKE_MAJOR%.exe"

But when I run a .bat with the code, it returns this error:

NUKE_VERSIONi was unexpected at this time.

Any insights into what is going on? I could just do this in python, but something this simple I shouldn't have to, right? Many thanks in advance.

Upvotes: 1

Views: 1856

Answers (1)

Mike C
Mike C

Reputation: 131

In a CMD Window a FOR-LOOP uses a single % sign as you have listed in your question.

In a Batch file a FOR-LOOP uses a double %% sign.

FOR /F "delims=v tokens=1" %%i IN ("%NUKE_VERSION%") DO set NUKE_MAJOR=%%i

Upvotes: 2

Related Questions