简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM