Variable not stay set

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Variable not stay set

#1 Post by SIMMS7400 » 03 May 2018 04:22

Hi Folks -

I have function that checks what day it is and sets it as a variable for a backup process.

The thing is, when I return to the mains script, the variable it not set.

Also, if i put the SETLOCAL ENABLEDELAYEDEXPANSION just before the call to the function, the function sets the day to Sunday, regardless of what day it is...Why wont SETLOCAL ENABLEDELAYEDEXPANSION work if it's set earlier on in the script?

Code: Select all

:GET_DOW
SETLOCAL ENABLEDELAYEDEXPANSION
::-- 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"

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Variable not stay set

#2 Post by Squashman » 03 May 2018 06:11

Your last line should be

Code: Select all

endlocal & SET "DOWName=%DOWName%"
Read this
viewtopic.php?f=3&t=6496

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

Re: Variable not stay set

#3 Post by SIMMS7400 » 03 May 2018 08:42

Squash -

Thank you so much - works like a charm!

Reading up on your link you provided now, thanks again!

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

Re: Variable not stay set

#4 Post by Compo » 04 May 2018 04:58

That method seems slightly complex! Would this not work for you, (it uses Call instead of implementing delayed expansion):

Code: Select all

:GET_DOW
Set "_WeekDays=Sunday Monday Tuesday Wednesday Thursday Friday Saturday"
For /F %%A In ('WMIC Path Win32_LocalTime Get /Value^|FindStr "[0-9]$"') Do Set "_%%A"
For /F "Tokens=1%_DayOfWeek%" %%A In (". . . . . . . . . %_WeekDays%") Do Set "_DayName=%%A"
For %%A In (Day Hour Minute Month Second) Do Call Set "_%%A=0%%_%%A%%" & Call Set "_%%A=%%_%%A:~-2%%"
GoTo :EOF
…where, you'd have access to the following variables, %_Day%, %_DayName%, %_DayOfWeek%, %_Hour%, %_Minute%, %_Month%, %_Quarter%, %_Second%, %_WeekDays%, %_WeekInMonth% and %_Year%

Also if all you're wanting to return is the name of the day of the week, why are you setting all of those variables when you only need one?

This should do what you need:

Code: Select all

:GET_DOW
For /F %%A In ('WMIC Path Win32_LocalTime Get DayOfWeek') Do For %%B In (
	Sunday=0 Monday.1 Tuesday.2 Wednesday.3 Thursday.4 Friday.5 Saturday.6
) Do If "%%~xB"==".%%A" Set "DOWName=%%~nB"
GoTo :EOF
…although it wouldn't even need calling, you could just add it in the correct location in your script, you could even have it as a single line if you preferred!:

Code: Select all

For /F %%A In ('WMIC Path Win32_LocalTime Get DayOfWeek') Do For %%B In (Sunday.0 Monday.1 Tuesday.2 Wednesday.3 Thursday.4 Friday.5 Saturday.6) Do If "%%~xB"==".%%A" Set "DOWName=%%~nB"

Post Reply