String manipulation & Variables

Hi there,

I forgot to get a variable when that variable name, contains a variable itself in the following batch file.

The output of the following batch file must be "12345 "12345".

The focus here is only line 11.

Thanks for your help.

Cheers,
Rene

@ECHO OFF

SETLOCAL EnableDelayedExpansion

CALL :SetLength

FOR %%A in (123456789) DO (
  SET Val=%%A
  SET Value=Value1
  SET Val1=!Val:~0,5!
  SET Val2=!Val:~0,%Trim[!Value!]%!
  ECHO "!Val1!" "!Val2!"
)
PAUSE
EXIT

:SetLength
SET Trim[Value1]=5
SET Trim[Value2]=6
EXIT /b

Open in new window

LVL 10
ReneGeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ZabagaRCommented:
11.  SET Val2=!Val:~0,5!
0
ReneGeAuthor Commented:
Hi ZabagaR

Thanks for your humour :)

Line 11 is about dynamic variables, not static.

Cheers,
Rene
0
oBdACommented:
Won't work if the index variable is a variable that's changed dynamically in the loop; it especially won't work with a nested index. What you can do is add another function that sets Val2:
@ECHO OFF
SETLOCAL EnableDelayedExpansion

CALL :SetLength

FOR %%A in (123456789) DO (
  SET Val=%%A
  SET Value=Value1
  SET Val1=!Val:~0,5!
  call :Trim Val2 "!Value!"
  ECHO "!Val1!" "!Val2!"
)
goto :eof

:SetLength
SET Trim[Value1]=5
SET Trim[Value2]=6
EXIT /b

:Trim
set Index=!Trim[%~2]!
set %1=!Val:~0,%Index%!
goto :eof

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

oBdACommented:
For the fun of it, a totally mindless example of what you were probably thinking about (no, it doesn't handle leap years):
@echo off
setlocal enabledelayedexpansion
set /a i = 0
for %%a in (January February March April May June July August September October November December) do (
	set /a i += 1
	set Month[!i!]=%%a
)
set /a i = 0
for %%a in (31 28 31 30 31 30 31 31 30 31 30 31) do (
	set /a i += 1
	set Days[!i!]=%%a
)
set /p Month=Enter a month [1-12]: 
echo !Month[%Month%]! has !Days[%Month%]! days.
echo All Months:
for /l %%i in (1, 1, 12) do (
	echo !Month[%%i]! has !Days[%%i]! days.
)

Open in new window

0
ReneGeAuthor Commented:
Thanks oBdA,

Your script is cool!!

I remember there was a way to create dynamic variables that looks like something like this:

SETLOCAL EnableDelayedExpansion
Set a=var1
Set Var1=1
ECHO !!!a!!!

Output=1

Thanks mate :)
0
ReneGeAuthor Commented:
0
oBdACommented:
Your example would have to look like that:
SETLOCAL EnableDelayedExpansion
Set a=var1
Set Var1=1
ECHO !%a%!

Open in new window

When using this array construction, you always have to combine the exclamation mark as outer expand character with either a static variable (that can be expanded with percent signs) or a loop variable as index. Batch does a non-greedy match of the expand characters, so !!a!! would not be read as !(!a!)!, but as (!!)a(!!).
0
Gerwin Jansen, EE MVETopic Advisor Commented:
C:\Temp>type dynvar.cmd
@echo off
setlocal enableDelayedExpansion

set a=var1
set var1=3

echo %a% = !%a%!
C:\Temp>dynvar
var1 = 3

C:\Temp>

<edit> Did not see previous post when submitting :(
0
ZabagaRCommented:
Some very useful stuff here. I figured by the time I had a chance to investigate further & post, some guru would have beaten me to it!

Regards
Z
0
ReneGeAuthor Commented:
Thanks for all your help.

My last example is my wrong.

Please remember that it must be in a FOR loop.

Therefore,  !%a%! when %a% is defined in the FOR loop would not work.

Cheers,
Rene
0
ReneGeAuthor Commented:
0
ReneGeAuthor Commented:
Problem solved.

The following from oBdA was my solution.

:Trim
set Index=!Trim[%~2]!
set %1=!Val:~0,%Index%!
goto :eof

Thanks you all!!

Cheers,
Rene
0
ReneGeAuthor Commented:
Also, thanks gerwinjansen for also providing the solution.

Cheers mates :)
0
Gerwin Jansen, EE MVETopic Advisor Commented:
Hi Rene, you're welcome - until the next one ;)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.