Weird function issue - variables not staying set

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Weird function issue - variables not staying set

#1 Post by SIMMS7400 » 25 May 2018 02:06

Hi FOlks -

Awhile back I ran into an issue where a variable isn't staying set when it returns from the function. Squash helped me out an got it working, but now the same script is still acting as it originally did and I have no idea why.

Essentially, from the main script, I call a function like such:

Code: Select all

SETLOCAL ENABLEDELAYEDEXPANSION

::-- Call function to determine Snapshot name for this session --::
CALL :GET_DOW
The function is as follows:

Code: Select all

:GET_DOW

::-- This parsing method works independently of any regional settings --::
::-- This works from Windows XP to Windows 2012R2 --::
::-- Adjust DOW variable to the required format ^[i.e. Sat or Saturday^]

SET "DOW=Sunday Monday Tuesday Wednesday Thursday Friday Saturday"
SET /A "LINE=0"

FOR /F "tokens=1-9" %%A in ('wmic Path Win32_LocalTime Get Day^,DayOfWeek^,Hour^,Minute^,Month^,Quarter^,Second^,WeekInMonth^,Year ^| FIND /v ""') DO (

  SET /A "LINE += 1"
  IF "!LINE!"=="1" ( SET "VARA=%%A" & SET "VARB=%%B" & SET "VARC=%%C" & SET "VARD=%%D" & SET "VARE=%%E"
					 SET "VARF=%%F" & SET "VARG=%%G" & SET "VARH=%%H" & SET "VARI=%%I")
					 
  IF "!LINE!"=="2" ( SET "!VARA!=%%A" & SET "!VARB!=%%B" & SET "!VARC!=%%C" & SET "!VARD!=%%D" & SET "!VARE!=%%E"
					 SET "!VARF!=%%F" & SET "!VARG!=%%G" & SET "!VARH!=%%H" & SET "!VARI!=%%I")
)

FOR %%A IN (Month Day Hour Minute Second) DO ( IF !%%A! LSS 10 SET %%A=0!%%A!)
SET /A DoWIndex = DayOfWeek + 1
FOR /F "tokens=%DoWIndex%" %%A IN ("%DOW%") DO SET "DOWName=%%A"

::-- Establish PBCS_PARAMS directory to house parameters --::
SET "PBCS_PARAMS=%APPDATA%\PBCS_PARAMS\"
IF NOT EXIST "%PBCS_PARAMS%" MKDIR "%PBCS_PARAMS%"

::-- Backup Name Management --::
SET "WK=Backup_Week"
SET "MO=Backup_Month"
SET "YR=Backup_Year"

::-- Daily Backup --::
SET "BKUP_DY=Backup_%DOWName%"

::-- Weekly Backup --::
IF "%WeekInMonth%"=="1" IF "%DayOfWeek%"=="1" SET "BKUP_WK=%WK%1"
IF "%WeekInMonth%"=="2" IF "%DayOfWeek%"=="1" SET "BKUP_WK=%WK%2"
IF "%WeekInMonth%"=="3" IF "%DayOfWeek%"=="1" SET "BKUP_WK=%WK%3"
IF "%WeekInMonth%"=="4" IF "%DayOfWeek%"=="1" SET "BKUP_WK=%WK%4"
IF "%WeekInMonth%"=="5" IF "%DayOfWeek%"=="1" SET "BKUP_WK=%WK%5"
IF "%WeekInMonth%"=="6" IF "%DayOfWeek%"=="1" SET "BKUP_WK=%WK%6"

::-- Monthly Backup --::
::-- The below line gets executed during initial set up - thereafter ignored --::
IF NOT EXIST "%PBCS_PARAMS%%MO%*" ECHO.>"%PBCS_PARAMS%%MO%1"& SET "BKUP_MO=%MO%1"& GOTO :SKIP
IF "%Day%"=="01" IF EXIST "%PBCS_PARAMS%%MO%1" DEL /F /Q "%PBCS_PARAMS%%MO%1"^
	& ECHO.>"%PBCS_PARAMS%%MO%2" & SET "BKUP_MO=%MO%2"
IF "%Day%"=="01" IF EXIST "%PBCS_PARAMS%%MO%2" DEL /F /Q "%PBCS_PARAMS%%MO%2"^
	& ECHO.>"%PBCS_PARAMS%%MO%3" & SET "BKUP_MO=%MO%3"
IF "%Day%"=="01" IF EXIST "%PBCS_PARAMS%%MO%3" DEL /F /Q "%PBCS_PARAMS%%MO%3"^
	& ECHO.>"%PBCS_PARAMS%%MO%4" & SET "BKUP_MO=%MO%4"
IF "%Day%"=="01" IF EXIST "%PBCS_PARAMS%%MO%4" DEL /F /Q "%PBCS_PARAMS%%MO%4"^
	& ECHO.>"%PBCS_PARAMS%%MO%5" & SET "BKUP_MO=%MO%5"
IF "%Day%"=="01" IF EXIST "%PBCS_PARAMS%%MO%5" DEL /F /Q "%PBCS_PARAMS%%MO%5"^
	& ECHO.>"%PBCS_PARAMS%%MO%6" & SET "BKUP_MO=%MO%6"
IF "%Day%"=="01" IF EXIST "%PBCS_PARAMS%%MO%6" DEL /F /Q "%PBCS_PARAMS%%MO%6"^
	& ECHO.>"%PBCS_PARAMS%%MO%1" & SET "BKUP_MO=%MO%1"
:SKIP

::-- Yearly Backup --::
IF "%Day%"=="01" IF "%Month%"=="1" SET "BKUP_YR=%YR%"

::-- Set all defined backup names into 1 variable for dynamic processing --::
SETLOCAL & SET "EXP=%BKUP_DY%,%BKUP_WK%,%BKUP_MO%,%BKUP_YR%"

GOTO :EOF
However, %EXP% isn't staying set. What am I doing wrong? Thanks!

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Weird function issue - variables not staying set

#2 Post by ShadowThief » 25 May 2018 04:05

Why even include the setlocal? I'm pretty sure it's causing a scope issue.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Weird function issue - variables not staying set

#3 Post by Compo » 25 May 2018 06:35

SIMMS7400, why are you still using that particular :DOW call?, don't you consider my advice worthy of reading?

Please take a look at this response where I showed you a simpler way of getting those variables!
…and there was no need or requirement for delayed expansion.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Weird function issue - variables not staying set

#4 Post by Ed Dyreen » 25 May 2018 21:49

SIMMS7400 wrote:
25 May 2018 02:06
However, %EXP% isn't staying set. What am I doing wrong? Thanks!
The problem is in these lines

Code: Select all

:GET_DOW
...
SETLOCAL & SET "EXP=%BKUP_DY%,%BKUP_WK%,%BKUP_MO%,%BKUP_YR%"
...
GOTO :EOF
When local scopes are used variables you intend to keep need to be explicitly pushed over the endlocal border for them to be visible from outside the scope in which they were defined. goto :eof does exactly as many implicit endlocals as there are unclosed scopes within the function it's ending.

Code: Select all

:myFunction
:: (
	setlocal
	:: (
		set "$name=DATA"
	:: )
	endlocal &set "$name=%$name%"
:: )
goto :eof

Post Reply