dbenham wrote: ↑13 Mar 2020 13:09
Why are you still explicitly expanding the variable? It works, but as I showed in my last post, that is not needed when using SET /A. You could use SET /A "YEAR=YEAR_NOW - 1"
dbenham wrote: ↑13 Mar 2020 13:09
Note that in this case you must explicitly expand the month value to get the correct result. SET /A "PREVMONTH=1MONTH-100" will NOT work.
I do believe you answered that yourself.
I just though that it will relieve the mental-load while reading and understanding that part of script.
dbenham wrote: ↑13 Mar 2020 13:09
FYI - No need to create a separate topic when this last question is obviously related to the previous.
Understood, won't happen again.
dbenham wrote: ↑13 Mar 2020 13:09
The ; at the end of the statement is causing the problem. SET /A sort of is expecting a second computation, although the proper symbol to use for multiple computations is a comma. Remove the semicolon and it works without error.
That's a mild mistake of mine, I'm glad I didn't put ; at the end of every statement.
Although would have helped to figure out the cause of this on my own.
dbenham wrote: ↑13 Mar 2020 13:09
Your way of dealing with dates is not good as it depends on the locale settings for the computer. For example, %date% on my machine yields Fri 03/13/2020 which completely breaks your code.
You should be using FOR /F with WMIC OS GET LOCALDATETIME to get the date and/or time in a way that can be parsed reliably. There is another WMIC option to get the various date/time components separately, but I cannot remember off the top of my head.
I would like to know if that's Windows 10 that you are using or one of the deprecated versions: Windows 8.1, Windows 8, Windows 7, Windows vista, Windows xp?
I'm kind of aware of this, but it is great that you pointed out. If the problem with date representation appears on Windows 10 I might consider WMIC.
However for simplicity I would like to keep using this method of gathering Date from operating system.
Not to mention that this is a port from Bash script to Batch script. I would like to keep both looking similarly for cross-platform readability if that's possible.
dbenham wrote: ↑13 Mar 2020 13:09
Your SET /A arithmetic will fail when you get a number like 08 or 09 because 0 prefixed values are treated as octal notation. A simple solution is to prefix the value with 1 and then subtract 100.
For example:
Code: Select all
set month=09
set /a "PrevMonth=1%month%-100"
Dave Benham
Now this is really great observation, would have not though about this until it has been too late.
Thanks Dave, will try to implement these changes tomorrow.