rossmcm
rossmcm

Reputation: 5620

Windows batch - access SET variables outside a routine

I'm trying to parse a file version of the form 1.2.3.4 into its component parts.

say I have file ParseVersion.bat

I want to call it with

call ParseVersion.bat 101.102.103.104

and in return get four variables called v1, v2, v3 and v4

my attempt

setlocal ENABLEDELAYEDEXPANSION
set Version=%1
set Version=%Version:.=,%

set j=1
for %%i in (%Version%) do (
  echo !j! %%i
  set /a j=!j!+1
  set v!j!=%%i  
)
endlocal

After calling this from another batch file, I want to be able to access %v1% through %v4% as environment variables. I guess the problem is due to the SET's inside a setlocal not persisting outside the setlocal, but how do I do this?

Upvotes: 0

Views: 293

Answers (1)

SachaDee
SachaDee

Reputation: 9545

Make a call :

@echo off 
set "$count=1"
set "$var=%1"
for %%a in (%$var:.= %) do call:next %%a
echo %var1% - %var2% - %var3% - %var4%
exit/b

:next
set "var%$count%=%1"
set /a $count+=1

Upvotes: 1

Related Questions