Page 1 of 1

Operation with number

Posted: 10 Sep 2013 02:26
by darioit
Hello,
I have this problem, make sum and subtraction with variable

This is my code:

Code: Select all

@echo on &setlocal
for /f "delims=" %%A in ('wmic OS Get localdatetime  ^| find "."') do set DATETIMEWMI=%%A
set Y=%DATETIMEWMI:~0,4%
set M=%DATETIMEWMI:~4,2%
set G=%DATETIMEWMI:~6,2%

IF %M%==01 (
    SET Y=%Y%-1
    SET M=12
    ) ELSE (
    SET /a M=%M%-1)


and this is the result:

E:\for /F "delims=" %A in ('wmic OS Get localdatetime | find "."') do set DATETIMEWMI=%A
set DATETIMEWMI=20130910095309.879000+120
E:\set Y=2013
E:\set M=09
E:\set G=10

E:\echo Y=2013
Y=2013

E:\echo M=09
M=09

E:\echo G=10
G=10

E:\IF 09 == 01 (
SET /A Y=2013-1
SET /A M=12
) ELSE (SET /A M=09-1 )
Invalid number. Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).


what's wrong?

Thanks in advance and Regards

Re: Operation with number

Posted: 10 Sep 2013 03:48
by foxidrive
SET Y=%Y%-1

the set above is missing /a

and if the number has a leading zero then it is treated as octal.

Use this but you'll also have to pad it out again with a leading zero.

Code: Select all

set /a x=1%m% - 1 -100

Re: Operation with number

Posted: 10 Sep 2013 05:45
by darioit
same problem

SET /A Y=2013-1
SET /A M=12
) ELSE (SET /A M=09-1 )
Invalid number. Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).

Re: Operation with number

Posted: 10 Sep 2013 06:04
by penpen
darioit wrote:same problem

SET /A Y=2013-1
SET /A M=12
) ELSE (SET /A M=09-1 )
Invalid number. Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).
With the same cause:
SET /A Y=2013-1
SET /A M=12
) ELSE (SET /A M=09-1 )
Invalid number. Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).

and with the same solution:
foxidrive wrote:

Code: Select all

set /a x=1%m% - 1 -100

penpen

Re: Operation with number

Posted: 10 Sep 2013 06:53
by darioit
Sorry I don't understand

IF %M%==01 (
SET /A YY=1%Y% -1 -100)
SET /A M=12
) ELSE (
set /a MM=1%m% - 1 -100)
echo %m%

Invalid number. Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).
Missing operator.
12

My goal is on date (AAAA-MM) 2013-09 trransform in last month 2013-08
if month is 01 transform in 2012-12

Best Regards

Re: Operation with number

Posted: 10 Sep 2013 07:09
by foxidrive

Code: Select all

@echo off
set m=01
set y=2013
call :here
set m=03
set y=2013
call :here
pause
goto :eof
:here
IF %M%==01 (
SET /A YY=%Y% - 1
SET /A MM=12
) ELSE (
set /a MM=1%m% - 1 -100
set yy=%y%
)

echo "%yy%" "%mm%"

Re: Operation with number

Posted: 10 Sep 2013 07:12
by foxidrive
This is an easier way to calculate dates.

Code: Select all

:: Date foward & backward
@echo off
if "%~2"=="" (
echo to get todays date use         call "%~n0" today 0
echo to get yesterdays date use     call "%~n0" today -1
echo to get 25 days before 19441213 call "%~n0" 1944/12/13 -25
echo to get 1250 days in the future call "%~n0" today 1250
echo.
echo Add a third parameter if you want a separator in the date string
echo EG: for this format YYYY-MM-DD using today's date
echo     call "%~n0" today 0 -
echo.
pause
goto :EOF)

set date1=%1
set qty=%2
set separator=%~3
if /i "%date1%" EQU "TODAY" (set date1=now) else (set date1="%date1%")
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%qty%,%date1%)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^&_
echo>>"%temp%\%~n0.vbs"         right(100+month(s),2)^&_
echo>>"%temp%\%~n0.vbs"         right(100+day(s),2)
for /f %%a in ('cscript //nologo "%temp%\%~n0.vbs"') do set result=%%a
del "%temp%\%~n0.vbs"
endlocal& set "YY=%result:~0,4%"&set "MM=%result:~4,2%"&set "DD=%result:~6,2%"
set "day=%YY%%separator%%MM%%separator%%DD%"
echo %%day%% is set to "%day%" (without the quotes)
echo %%YY%% is set to %YY%
echo %%MM%% is set to %MM%
echo %%DD%% is set to %DD%

Re: Operation with number

Posted: 10 Sep 2013 12:32
by aGerman
If you only want to subtract one month the following should be sufficient

Code: Select all

@echo off &setlocal
for /f %%i in ('wmic path Win32_LocalTime get Year^,Month /value^|findstr .') do set /a %%i
if %Month%==1 (set /a "Month = 12, Year -= 1") else set /a "Month -= 1"
if %Month% lss 10 set "Month=0%Month%"
echo %Year%-%Month%
pause

Regards
aGerman

Re: Operation with number

Posted: 10 Sep 2013 13:06
by penpen
darioit wrote:Sorry I don't understand

IF %M%==01 (
SET /A YY=1%Y% -1 -100)
SET /A M=12
) ELSE (
set /a MM=1%m% - 1 -100)
echo %m%

Invalid number. Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).
Missing operator.
12

My goal is on date (AAAA-MM) 2013-09 trransform in last month 2013-08
if month is 01 transform in 2012-12

Best Regards
Sorry, if i sounded too offense, or too rude, i don't wanted to:
The problem is the code part "set /a MM=%m% - 1", because %m% can hold the values 08 or 09.
numbers with trailing 0's are interpreted as octal values, but the digits 8 and 9 are no octal digits, so this error is shown.
The workaround places a non-zero digit in front of the problematic value, and then you have to substract the raised number,
in this case the raised value is 100.
As the year as no trailing zero there is no need, to change it this way, but it should be no problem, to do so, if you want,
but the raised value is in this case the 10000.

So the corrected code is nearly what you've then posted:

Code: Select all

@echo on &setlocal
for /f "delims=" %%A in ('wmic OS Get localdatetime  ^| find "."') do set DATETIMEWMI=%%A

set Y=%DATETIMEWMI:~0,4%
set M=%DATETIMEWMI:~4,2%
set G=%DATETIMEWMI:~6,2%

IF %M%==01 (
    SET Y=%Y%-1
    SET M=12
    ) ELSE (
    SET /a M=1%M%-101)

goto :eof

penpen