Tricks with setx?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Tricks with setx?

#1 Post by SIMMS7400 » 05 Oct 2017 17:16

Hi Folks -

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!

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Tricks with setx?

#2 Post by SIMMS7400 » 06 Oct 2017 11:04

Hi folks -

This is how I handled the month piece - sloppy I know - so I was wondering if there is a cleaner way?

Code: Select all

SET "SKIP="
IF NOT EXIST "%TEMP%\Backup_Month*" ECHO.>"%TEMP%\Backup_Month1" & SET "BKUP_MO=Backup_Month1" & SET "SKIP=1"
IF NOT DEFINED SKIP (
   IF "%Day%"=="01" IF EXIST "%TEMP%\Backup_Month1" DEL /F /Q "%TEMP%\Backup_Month1" & ECHO.>"%TEMP%\Backup_Month2" & SET "BKUP_MO=Backup_Month2"
   IF "%Day%"=="01" IF EXIST "%TEMP%\Backup_Month2" DEL /F /Q "%TEMP%\Backup_Month2" & ECHO.>"%TEMP%\Backup_Month3" & SET "BKUP_MO=Backup_Month3"
   IF "%Day%"=="01" IF EXIST "%TEMP%\Backup_Month3" DEL /F /Q "%TEMP%\Backup_Month3" & ECHO.>"%TEMP%\Backup_Month4" & SET "BKUP_MO=Backup_Month4"
   IF "%Day%"=="01" IF EXIST "%TEMP%\Backup_Month4" DEL /F /Q "%TEMP%\Backup_Month4" & ECHO.>"%TEMP%\Backup_Month1" & SET "BKUP_MO=Backup_Month1"
)

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

Re: Tricks with setx?

#3 Post by aGerman » 06 Oct 2017 12:00

Working with a file where you find all the settings is imho a good idea. What I'm wondering is ...
You're talking about databases all the time. So why don't you use a database for your settings? SQLite has a nice little command line utility (about 500 KB) that you can use with normal SQL statements to create a database file, add tables, read and write values etc. I wouldn't use the %temp% folder though. Rather create a subfolder in %appdata%.
Another possibility would be to use the registry (which actually is nothing else but a database). There is no mystery in using REG ADD and REG QUERY. Also even if you can't update the environment of the running processes, the values that you added using SETX /M can be found in HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment.

Steffen

Post Reply