Values of variables after ENDLOCAL

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
miskox
Posts: 609
Joined: 28 Jun 2010 03:46

Values of variables after ENDLOCAL

#1 Post by miskox » 13 Jun 2024 00:53

Instead of this code:

Code: Select all

set wkday[1]=Sun
set wkday[2]=Mon
set wkday[3]=Tue
set wkday[4]=Wed
set wkday[5]=Thu
set wkday[6]=Fri
set wkday[7]=Sat
I thought I would use this:

Code: Select all

@echo off
setlocal ENABLEDELAYEDEXPANSION
set /a cnt=1
for %%f in (Sun Mon Tue Wed Thu Fri Sat) do set wkday[!cnt!]=%%f&set /a cnt+=1

echo defined
set wkday
endlocal
echo not defined after ENDLOCAL
set wkday
Output:

Code: Select all

c:\array.cmd
defined
wkday[1]=Sun
wkday[2]=Mon
wkday[3]=Tue
wkday[4]=Wed
wkday[5]=Thu
wkday[6]=Fri
wkday[7]=Sat
not defined after ENDLOCAL
Environment variable wkday not defined

c:\
I use this Foxi's code: viewtopic.php?p=33530#p33530

Code: Select all

@echo off
set day=1
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)
for /f %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
del "%temp%\%~n0.vbs"
set "YYYY=%result:~0,4%"
set "MM=%result:~4,2%"
set "DD=%result:~6,2%"
set "data=%yyyy%-%mm%-%dd%"
echo Tomorrow is "%data%"
I can't use WeekdayName function because I need weekday names in Slovene.

I just can't find a post (I am sure there should be one) regarding this. I don't want to use delayed expansion after this FOR loop.

What should I do?

Thanks.
Saso

aGerman
Expert
Posts: 4674
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Values of variables after ENDLOCAL

#2 Post by aGerman » 13 Jun 2024 12:59

If I was you I would not overcomplicate things. Something like that is good enough, isn't it?

Code: Select all

for %%I in ("[1]=Sun" "[2]=Mon" "[3]=Tue" "[4]=Wed" "[5]=Thu" "[6]=Fri" "[7]=Sat") do set "wkday%%~I"
However, if that was rather boring and you want to have a little fun, this may do the trick for you:

Code: Select all

for /f "tokens=1,2" %%i in ('(for %%I in (Sun Mon Tue Wed Thu Fri Sat^) do @echo( %%I^)^|find /n /v ""') do set "wkday%%i=%%j"
Steffen

miskox
Posts: 609
Joined: 28 Jun 2010 03:46

Re: Values of variables after ENDLOCAL

#3 Post by miskox » 13 Jun 2024 13:17

This is why you are an expert.

Thank you!

Saso

Aacini
Expert
Posts: 1900
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Values of variables after ENDLOCAL

#4 Post by Aacini » 13 Jun 2024 17:11

This works:

Code: Select all

@echo off
setlocal

set /a cnt=1
for %%f in (Sun Mon Tue Wed Thu Fri Sat) do call set wkday[%%cnt%%]=%%f&set /a cnt+=1

set wkday
Antonio

miskox
Posts: 609
Joined: 28 Jun 2010 03:46

Re: Values of variables after ENDLOCAL

#5 Post by miskox » 13 Jun 2024 23:01

Thank you Aacini. I tried to make such solution but I guess I inserted too many or too little %s.

Saso

Post Reply