As you know, with setx, you're unable set and use that variable during the same session. However, I was wondering if anyone had any tricks to do so?
I have a script that I use to trigger certain exports within an application. Recently, we've predefined more exports and therefore, I needed to create a function to derive the various export names that I pass back to the application.
I am writing a script to trigger the following exports:
Code: Select all
Backup_Monday
Backup_Tuesday
Backup_Wednesday
Backup_Thursday
Backup_Friday
Backup_Saturday
Backup_Sunday
Backup_Week1
Backup_Week2
Backup_Week3
Backup_Week4
Backup_Week5
Backup_Week6
Backup_Month1
Backup_Month2
Backup_Month3
Backup_Month4
Backup_Month5
Backup_Month6
Backup_Year
Based on the above, I'm able to set the correct export names into variables easily except for Month. Reason being, there's not I can use in order to derive Month1-4. Therefore, I decided to try to emply setx to retain that variable name until the next month where it would advance to Month2...Then, after Month4, it would return to Month1.
Here is my function:
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"
SET "BKUP_DY=Backup_%DOWName%"
IF "%WeekInMonth%"=="1" IF "%DayOfWeek%"=="1" SET "BKUP_WK=Backup_Week1"
IF "%WeekInMonth%"=="2" IF "%DayOfWeek%"=="1" SET "BKUP_WK=Backup_Week2"
IF "%WeekInMonth%"=="3" IF "%DayOfWeek%"=="1" SET "BKUP_WK=Backup_Week3"
IF "%WeekInMonth%"=="4" IF "%DayOfWeek%"=="1" SET "BKUP_WK=Backup_Week4"
IF "%WeekInMonth%"=="5" IF "%DayOfWeek%"=="1" SET "BKUP_WK=Backup_Week5"
IF "%WeekInMonth%"=="6" IF "%DayOfWeek%"=="1" SET "BKUP_WK=Backup_Week6"
IF ["%BKUP_MO%"] == [""] echo SETX /m BKUP_MO Backup_Month0>SETX.cmd && CALL SETX.cmd
IF "%Day%"=="01" IF "%DayOfWeek%"=="1" IF "%BKUP_MO%"=="Backup_Month0" SETX /m BKUP_MO Backup_Month1
IF "%Day%"=="01" IF "%DayOfWeek%"=="1"IF "%BKUP_MO%"=="Backup_Month1" SETX /m BKUP_MO Backup_Month2
IF "%Day%"=="01" IF "%DayOfWeek%"=="1"IF "%BKUP_MO%"=="Backup_Month2" SETX /m BKUP_MO Backup_Month3
IF "%Day%"=="01" IF "%DayOfWeek%"=="1"IF "%BKUP_MO%"=="Backup_Month3" SETX /m BKUP_MO Backup_Month4
IF "%Day%"=="01" IF "%Month%"=="1" SET "BKUP_YR=Backup_Year"
SET "EXP=%BKUP_DY%,%BKUP_WK%,%BKUP_MO%,%BKUP_YR%"
FOR %%E IN ( %EXP:,= % ) DO ECHO "%%E"
pause
GOTO :EOF
I thought about deriving the correct value, but then spooling it to a temp cmd, executing it, and then when I use it in the script, it will be set and ready to go - however that doesn't seem to work. Looking for ideas.
Also, the issue with using the global variable is it will always be set, which is not good. So I essentially need to set it, execute it in my export utility, then un-set it all within the same session.
I set all the possible export names into EXP comma delimited so then I just run it through a for loop as such:
Code: Select all
>>%LOGFILE% (
ECHO.
ECHO ********************************************************
ECHO Export PBCS Backup Snapshot^(s^)
ECHO ********************************************************
ECHO.
ECHO Action : exportsnapshot
ECHO Export^(s^): %EXP%
ECHO --------------------------------------------------------
ECHO.
)
ECHO Exporting PBCS Snapshot^(s^)...
PING -n 2 127.0.0.1>nul
FOR %%P IN ( %EXP:,= % ) DO (
CALL "%EPMAUTOMATE%" exportsnapshot "%%~P" && (
ECHO Export PBCS '%%~P' Snapshot : Successful >>%LOGFILE% ) || (
ECHO Export PBCS '%%~P' Snapshot : Failed >>%LOGFILE%
GOTO AbnormalExit
)
)
Thanks!