0

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.

Joe Marfice
  • 151
  • 2
  • 18
  • 3
    Variables created by `SETX` are not available in the current environment. From the help file: **2) On a local system, variables created or modified by this tool will be available in future command windows but not in the current CMD.exe command window.** – Squashman Mar 01 '19 at 16:34
  • 2
    Just put this line: `Set DateTimeCode=%DateCode%_%TimeCode%` below the `Setx` one... – Aacini Mar 01 '19 at 16:37
  • 2
    Possible duplicate of [how do I retrieve a value after using setx?](https://stackoverflow.com/questions/24059311/how-do-i-retrieve-a-value-after-using-setx). Your provided code does not indicate that you have understood or taken account of the accepted answer at the linked answer in your question body. It specifically states that 'edits will only take effect when a new command window is opened - they do not affect the current CMD or PowerShell session'. Your code does not attempt to read that value in a new CMD session, hence it is a duplicate of your provided linked question. – Compo Mar 01 '19 at 17:19
  • Compo, I thought I made it clear: the edit DOES NOT take effect when a new command window is opened. I did understand and take account of that answer, which doesn't seem to be correct in this instance: "every time it starts it echoes the same date-time". – Joe Marfice Mar 01 '19 at 18:20
  • @Squashman, the variables I modified by this tool was NOT available in future command windows. I know this is confusing to you guys, but for clarity: I run the batch file. It prints out the wrong date (from January) at the beginning. It prints out the current date at the end. I close its window. I run the batch file AGAIN. It prints out the wrong date (from January) at the beginning. It prints out the current date at the end. I close its window. [repeat] – Joe Marfice Mar 01 '19 at 18:24
  • @Aacini: Thanks, but your suggestion doesn't change the behavior. – Joe Marfice Mar 01 '19 at 18:27
  • Can you provide us with a better representation of your issue, because from what you've posted, `1.` script runs, `2.` 'global' variable value is output, _(which shows it was last set on January 22nd 2019)_, `3.` 'local' variable value is set for todays date and time, `4.` 'global' variable value is set to todays date and time, `5.` 'local' variable value is output, _(which shows it was last set on March 3rd 2019)_. All of that is absolutely to be expected, so you need to show us the part of the process which isn't working as intended, _i.e. What happens after what you've shown us?_ – Compo Mar 01 '19 at 18:59
  • Compo, EVERY time I rerun the batch file it claims the variable DateTimeCode is "190122_190410". Also, every time SyncBack uses that variable, it shows the value "190122_190410". Also, every time I open a brand-new Cmd window - even after rebooting, even after rerunning the batch file over and over again - it shows the value "190122_190410". I don't care what the value is displayed as at the end of the batch file run; I care that the ONLY value I can get from the system for that variable is "190122_190410". – Joe Marfice Mar 01 '19 at 19:06
  • Please note, I have seen your latest edit, and it does not improve your question. Your question must provide us with sufficient information for us to exactly replicate your issue, otherwise we'd be guessing, _and that isn't what a technical site does!_ Please also note that deliberately using upper case strings is shouting, shouting does not improve matters with regard us replicating your problem, what it does is reduces your possibilities of receiving help! – Compo Mar 01 '19 at 19:10
  • " i.e. What happens after what you've shown us?" - the system apparently "throws away" the new value, and reverts to the value stored for that variable made two months ago. My apologies for "shouting"; I was trying to emphasize how things were /not/ behaving as expected. – Joe Marfice Mar 01 '19 at 19:12
  • I'll also ask that you revisit your question area and include in it only the information necessary for us to help. If the code and information you've posted there does not allow for us to exactly replicate your issue, do not include it. Also please don't use the comment are for adding information, many members do not search the comments when providing help! – Compo Mar 01 '19 at 19:19
  • I can't replicate your problem. I used your code at the top of your question and the variable created by setx is always available and updated in a new cmd window. – Squashman Mar 01 '19 at 20:20
  • Why don't you just use a text file to store the value? Instead of a persistent `setx` variable... – Aacini Mar 01 '19 at 20:36
  • Squashman, thanks for the feedback. I tried it at home, and it works on my home computer, too. Something stupidly wrong with the way my work computer - which is about to die, anyway. Anyway, taking Aacini's advice, but it bugged me that nothing seemed to work the way everyone said it would. Somehow I just found a bug, that isn't reproducible. Thanks to everyone who did more than criticize. – Joe Marfice Mar 02 '19 at 20:45

0 Answers0