Computing.Net > Forums > Disk Operating System > Math in Batch File

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Math in Batch File

Reply to Message Icon

Name: WBaker
Date: June 3, 2003 at 09:39:12 Pacific
OS: win 2000
CPU/Ram: 1.1 256k
Comment:

within a batch file, Is there a way to set a variable to be equal to the system date minus seven days?



Sponsored Link
Ads by Google

Response Number 1
Name: bitbyte
Date: June 3, 2003 at 10:23:29 Pacific
Reply:


i wrote a small calculater for dos, you can use it in a batchfile, if you firm with batchfile programming, you can use it for that problem, if not -> mail me
download SCALC.COM under downloads from http://plop.at


0

Response Number 2
Name: Secret_Doom
Date: June 3, 2003 at 13:17:07 Pacific
Reply:

Bitbyte's SCALC.COM is a great program, useful on batch files under DOS/Win9x. However, in NT systems such as Win 2000, the command prompt itself can do all it does (and much more). Here's a sample:

set DAY=30
set /A DAY -= 7

For more information about doing arithmetic operations with the SET command, type "SET/?" on the command prompt.

However, that still doesn't solve all your problem, that's actually the easy part. You gotta get the date, subtract 7 from the day, and do the necessary calculations if the new resulting day is less than 1. Such calculations aren't that simple, you gotta calculate the new month, calculate the new date based on that, sometimes check if the year has a 29-day-february, etc. The following batch file will do it:

@echo off

for /F "tokens=2-4 delims=/- " %%A in ('date/T') do (
    set M=%%A
    set D=%%B
    set Y=%%C
)

if "%D:~0,1%"=="0" set D=%D:~1%
if "%M:~0,1%"=="0" set M=%M:~1%

:: Subtract the desired number of days
set /A D -= 7

:arrange
if %D% GTR 0 goto dateOK
set /A M -= 1
set /A BX = %Y% %% 4
if %M% GTR 0 goto keepyear
set /A Y -= 1
set /A M += 12
:keepyear
for %%? in (1 3 5 7 8 10 12) do if %M% == %%? set /A D += 31
for %%? in (4 6 9 11) do if %M% == %%? set /A D += 30
if %M% == 2 (
    if %BX% EQU 0 set /A D += 29
    if %BX% NEQ 0 set /A D += 28
)
goto arrange
:dateOK
if "%D:~1%"=="" set D=0%D%
if "%M:~1%"=="" set M=0%M%

The variables are M D and Y, with month and date with 2 digits and year with 4.

-- Leonardo Pignataro - Secret_Doom --

secret_doom@hotmail.com
www.batch.hpg.com.br

__________________________________________________________________


0

Response Number 3
Name: SkipCox
Date: June 3, 2003 at 13:18:27 Pacific
Reply:

Nice site bitbyte.

Also try:

MrHall


0

Response Number 4
Name: bitbyte
Date: June 3, 2003 at 14:25:20 Pacific
Reply:


to SkipCox

thanks, i looked at yours, a lot of interesting calculation programs


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Disk Operating System Forum Home


Sponsored links

Ads by Google


Results for: Math in Batch File

What does the % mean in batch files? www.computing.net/answers/dos/what-does-the-mean-in-batch-files/5925.html

Variables in batch files www.computing.net/answers/dos/variables-in-batch-files/12289.html

Choice in batch files on NT4-DOS www.computing.net/answers/dos/choice-in-batch-files-on-nt4dos/11474.html