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
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