My goal is to pass to SyncBack (Pro version) a directory path to create that contains the current date&time in its name, as "D:\YYMMDD_hhmmss".
The following batch file stores a variable named DateTimeCode with the formatted string, reports success storing the variable, and then echoes back a version of the variable that is a month old.
@ Echo OFF
Echo DateTimeCode=%DateTimeCode%
Set DateCode=%Date:~-2,2%%Date:~-10,2%%Date:~-7,2%
Set TimeCode=%Time:~0,2%%Time:~3,2%%Time:~6,2%
Setx DateTimeCode %DateCode%_%TimeCode%
Echo DateTimeCode=%DateTimeCode%
Pause
I thought this problem was just in SyncBack, but when I run the batch file independently it gets the same result. It looks like it works, but every time it starts it echoes the same date-time ("190122_190410") - it doesn't change at the beginning, even though it's changed at the end. Next time: new ending value for %DateTimeCode%, but it will begin with Echo %DateTimeCode% producing "190122_190410".
And SyncBack is using that outdated value to create the directory.
Feels very much like a context/profile issue, except that the two Echo statements are in the same profile and context; they differ only in where in the Batch file they exist. Here's an example of the output:
DateTimeCode=190122_190410
SUCCESS: Specified value was saved.
SUCCESS: Specified value was saved.
SUCCESS: Specified value was saved.
DateTimeCode=190301_111443
Press any key to continue . . .
Perhaps related to this question, except that duplicate Set/Setx statements didn't fix my problem:
@ Echo OFF
Echo DateTimeCode=%DateTimeCode%
Set DateCode=%Date:~-2,2%%Date:~-10,2%%Date:~-7,2%
Set TimeCode=%Time:~0,2%%Time:~3,2%%Time:~6,2%
Set DateTimeCode=%DateCode%_%TimeCode%
Setx DateCode %Date:~-2,2%%Date:~-10,2%%Date:~-7,2%
Setx TimeCode %Time:~0,2%%Time:~3,2%%Time:~6,2%
Setx DateTimeCode %DateCode%_%TimeCode%
Echo DateTimeCode=%DateTimeCode%
Pause
(Note that Set requires an equals-sign, while Setx requires a space between the arguments.)
Can anyone help me uncover the source of my bug?
EDIT: People don't seem to understand the real point here. The variable REVERTS to a value stored last January, EVERY TIME. I don't care what the value is during the Batch file run, but by the next time I run it (in a new Cmd window) it should have changed. It hasn't changed - and I've run it a dozen times since January.