dates in batch script

July 11, 2012 at 00:22:39
Specs: Windows 7
I have two actions I wish to complete.

1. Get todays date in the format of yyyymmdd and > now.txt

2. Get todays date in the same format, add a number varible of (e.g 20) then > then.txt


See More: dates in batch script

Report •

#1
July 11, 2012 at 01:47:21
vbscript solutions are much better for date math. Here's some major over-kill I wrote a couple years ago, but it cheats by using "debug" to get system date:

@goto :AAA
e 100 8A 1E 82 00 80 CB 20 B4 2A CD 21 80 FB 77 74 10
e 110 88 C8 80 FB 79 74 09 88 F0 80 FB 6D 74 02 88 D0
e 120 B4 4C CD 21 03 B4 4C CD 21
RCX
30
W
Q
:above code is nonviral! just does system-calls to get date.
:: ******************** DATE BUSTER!!! *************************
:: ************ THE UNIVERSAL DATEMATH FOR BATCH!!!! *************
:: add or subtract any no. of days from either current date or given date!
:: generate current date then decrement by 1 day and normalize output.
::With no "setlocal", the script can be called from another script which can
::access the var.s via the env.table
:AAA
@echo off
if not exist dmy.com debug dmy.com < %0.bat >nul 2>nul
if not defined dlm set dlm=-
if "%1"=="" (set sub=0) else (set sub=%1)
if "%2" neq "" goto :indate
dmy d
set day=0%errorlevel%
set day=%day:~-2%
dmy m
set month=0%errorlevel%
set month=%month:~-2%
dmy y
set /a year=%errorlevel%+1792
goto :start
:indate
set xx=/%2
set xx=%xx:-=/%
set xx=%xx:/0=/%
for /f "tokens=1-3 delims=/" %%m in ("%xx%") do (
set month=%%m
set day=%%n
set year=%%o
)

:start
:: build table of month-end values
for /L %%a in (1 1 12) do set /a nn%%a=30+"(%%a+(%%a/8))%%2"

echo.starting from (yyyymmdd): %year%%month%%day%
:pause
if "%sub:~0,1%" equ "-" (
set adj=subdate
set /a sub=0-sub
) else (
set adj=adddate
)
for /L %%a in (1 1 %sub%) do call :%adj%
set month=0%month%
set month=%month:~-2%
set day=0%day
set day=%day:~-2%
:set ddate=%month%%dlm%%day%%dlm%%year%
set ddate=%year%%month%%day%
echo finished: %ddate%
goto :eof

:subdate
set /a day-=1
if %day% neq 0 goto :show
set /a test=31
set /a month="(%month%+10)%%12+1"
call :%month% 2>nul
if %month% equ 12 set /a year-=1
set /a day=test
goto :show

:adddate
set /a day+=1
set /a test=31
call :%month% 2>nul
if %day% leq %test% goto :show
set /a day=1
set /a month="(%month%+12)%%12+1"
if %month% equ 1 set /a year+=1
goto :show

:4
:6
:9
:11
set /a test-=1
goto :eof
:2
set /a test=28
set /a leap="year%%4"
if %leap% equ 0 set /a test+=1
goto :eof


:show
echo.%month% %day% %year%


Report •

#2
July 11, 2012 at 02:32:14
Here's a VB script for you to test. I see no purpose in adding a number to a date, this does not produce a new date as the original date is handled as a decimal number and the result could be say 20120735 which is not a valid date. However, that's what you asked for.

Good luck.

@echo off
cls
setlocal

:: Syntax - Filename.bat n where n is the number to be added to today's date
::          If a negative number is entered that number will be deducted
::          from today's date

set vbs=%temp%\vbs.vbs
(
echo otherdate = (Date(^)^)
echo   yy = datePart("yyyy", otherdate^)
echo   mm = datePart("m"   , otherdate^)
echo   dd = datePart("d"   , otherdate^)
echo wscript.echo yy^&" "^&mm^&" "^&dd
)>%vbs%

FOR /F "tokens=1-3" %%A in ('cscript //nologo %vbs%') do (
        set year=%%A
        set month=%%B
        set day=%%C

)
del %vbs%

if %month% lss 10 set month=0%month%
if %day%   lss 10 set day=0%day%

echo %year%%month%%day%>now.txt

set /a then=%year%%month%%day%+%1
echo %then%>then.txt

type now.txt then.txt


Please come back & tell us if your problem is resolved.


Report •

#3
July 11, 2012 at 02:37:22
your right I did, You also right i was having a blond moment.

I currently use an exe to process the calulation. It would need to calulate the date as 20 days in advance..

sorry.....


Report •

Related Solutions

#4
July 11, 2012 at 04:36:18
Here's another to test....

@echo off
cls
setlocal

:: Syntax: Filename[.bat] n where n is the number of days to be added to
::         today's date.   If a negative number is entered that number of
::         days will be deducted from today's date.   If a number of days
::         is not entered a compilation error will occur and the output will
::         be corrupt.

set vbs=%temp%\vbs.vbs

:: Get today's date.................
set var=0
call :vbs
echo %year%%month%%day%>now.txt

:: Get today's date plus n..........
set var=%1
call :vbs
echo %year%%month%%day%>then.txt

del %vbs%

type now.txt   then.txt
exit /b


:vbs
(
echo otherdate=(Date(^)+%var%^)
echo yyyy=datePart("YYYY", otherdate^)
echo   mm=datePart("M"   , otherdate^)
echo   dd=datePart("D"   , otherdate^)
echo Wscript.Echo yyyy^&" "^&mm^&" "^&dd
)>%vbs%

FOR /F "tokens=1-3" %%A in ('cscript //nologo %vbs%') do (
        set year=%%A
        set month=%%B
        set day=%%C
)

if %month% lss 10 set month=0%month%
if   %day% lss 10 set   day=0%day%


Please come back & tell us if your problem is resolved.


Report •

#5
July 11, 2012 at 06:49:45
thanks for the post. I can't see a split. is this two files?

Report •

#6
July 11, 2012 at 10:13:30
I would also recommend a VBScript aided solution as there is no debugger in Windows 7 anymore (by default).

Wahine's solution works well.

http://blogs.msdn.com/b/tess/archiv...

Tony


Report •

#7
July 11, 2012 at 12:16:25
nbrane,

I think your debug script needs:

N day.com


=====================================
Life is too important to be taken seriously.

M2


Report •

#8
July 11, 2012 at 14:13:41
@kwadman thanks for the post. I can't see a split. is this two files?

Must be my turn to have a blond day, I don't understand your question, you can't see a split what, and is what two files?

Two files are created as requested by you, now.txt contains today's date in the format yyyymmdd, then.txt contains today's date plus n days again in the format yyyymmdd. At least that's what my script is supposed to achieve. Forum member Tonysathre kindly comments that my script works well. Please don't forget that you are in a testing environment until you prove the script or otherwise and come back to define any problem you have encountered.

Can you not find the created files using the Dir command?


Please come back & tell us if your problem is resolved.


Report •

#9
July 11, 2012 at 18:43:04
@M2go: I think the debug commandline handled the filename:
if not exist dmy.com debug dmy.com < %0.bat >nul 2>nul

Man, that sucks not having debugger anymore. That really sucks. I hope I can continue to run my older versions. It can be very handy at times.

I'd also written a vbs for dates, but it looks as though the ones posted are doing the job nicely, so I'll "go away"


Report •

#10
July 11, 2012 at 19:21:37
@nbrane I'd also written a vbs for dates, but it looks as though the ones posted are doing the job nicely, so I'll "go away"

Care to share? No doubt yours is more efficient than my effort.


Please come back & tell us if your problem is resolved.


Report •

#11
July 11, 2012 at 19:40:16
@wahine: i doubt it... but it worked for me!
This one wants the date (or time) first, then the number to add/subtract (if neg).
like: daa 12/31/2011 1

set xin=wscript.stdin
set cout=wscript.stdout
dim y,x
set y=wscript.arguments
a=y.count-1
if a<1 then
x=xin.readline
y=split(x)
a=ubound(y)
end if
'use colon to interpret as time, else date
'cout.writeline(a&" "&y(a))
p=instr(y(a-1),":")
'sets defaults for date (adjust no. days) or time (adjust no. minutes)
if p>0 then s="n" else s="d"
i=0
'now allow overrides by token #1 if valid subset
if instr("dmwhns",lcase(y(0)))>0 then
s=y(0)
i=1
end if
'cout.writeline(s&" "&y(i)&" "&y(i+1))
test=dateadd(s,y(i+1),y(i))
cout.write(test)


Report •

#12
July 11, 2012 at 20:00:49
Hi nbrane,

You're right about the script. Senior moment.

I have a debug which is version indifferent. At least up to and including XP..


=====================================
Life is too important to be taken seriously.

M2


Report •

#13
July 12, 2012 at 18:23:45
nbrane - thanks for sharing your interesting VB script.


Please come back & tell us if your problem is resolved.


Report •

#14
July 13, 2012 at 00:05:55
The code i am trying to write is in a batch file

So the batch file is processing....
we know days_increase=20

Your all saying use VB, fine. So now i have to post this "20" to the VB (split file)
VBS processing.........
Finished...
Batch continues.........
The batch file should now know

days_increase=20
today=20121110
due_date=20121120


How would i complete this?


Report •

#15
July 13, 2012 at 20:04:11
@echo off & setlocal enabledelayedexpansion
:this creates the vbs one-liner each time the batch is run...
>da20.vbs echo wscript.stdout.writeline(date)&" "dateadd("d",date,wscript.arguments(0))

:here's your target, flexible for down-the-road
set add=20
set z=1
for /f "tokens=1,2" %%a in ('da20.vbs %add%') do (
call :format(%%a)
set /a z+=1
call :format(%%b)
)
> now.txt echo %out1%
> then.txt echo %out2%
goto :eof

:format
for /f "tokens=/" %%a in ("%1") do (
set out%z%=%%c
set x=0%%a
set x=!x:~-2!
set out%z%=out%z%!x!
set x=0%%b
set x=!x:~-2!
set out%z%=out%z%!x!
:end

this should get the two items you want in format you want, hopefully, maybe...


Report •

Ask Question