Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Need to loop through a command from date 1 through date 2

989 views
Skip to first unread message

Norm Hickel

unread,
Oct 28, 2002, 9:57:52 PM10/28/02
to
I have one last date problem... and thanks very much to those who have been
so helpful thus far... this ng has been a terrific resource.
I need to be able to take a given date like 20021001 (Nov 1, 2002) and have
it incremented by 1 until it reaches a specific date like 20021009 (Nov, 9
2002). Or it could be any set of dates. The trick is being able to start
with a given date and increment it by 1 taking into account month and year
changes. What I'm trying to accomplish is creating a script which can be
called with a specific start and stop date and then having that script loop
through a given command as many times as necessary while using the newly
incremented date as a parameter to a JAVA application that uses the date in
an extraction process.
Anyone have any good ideas on how to deal with this issue???

Thanks in advance for any suggestions
Norm


Phil Robyn

unread,
Oct 29, 2002, 12:01:45 AM10/29/02
to

Ritchie previously posted an extremely nice DATEMATH batch file, which
you could use to do this *if* you have Win2000 or WinXP.

<Win2000> c:\cmd>demo\DoFromTo 20021001 20021009
java YourClass 20021001
java YourClass 20021002
java YourClass 20021003
java YourClass 20021004
java YourClass 20021005
java YourClass 20021006
java YourClass 20021007
java YourClass 20021008
java YourClass 20021009

=====begin c:\cmd\demo\DoFromTo.cmd====================
1. @echo off
2. set date_parm=%1
3. set stop_date=%2
4. :next_day
5. echo java YourClass %date_parm%
6. call DATEMATH %date_parm% +1
7. set date_parm=%DATEMATH%
8. if %date_parm% GTR %stop_date% goto :EOF
9. goto :next_day
=====end c:\cmd\demo\DoFromTo.cmd====================

Here's a slightly modified version of Ritchie's DATEMATH batch
file, along with a general-purpose XHELP file:

=====begin C:\CMD\TEST\datemath.cmd====================
01. @echo off&setlocal&set cd=
02. ::
03. :: Syntax: [ yyyymmdd OR today ] [+ OR -]nnnnn [.]
04. ::
05. :: where 'nnnnn' is the number of days to add or subtract
06. ::
07. :: Examples: %~n0 20010501 +234 .
08. :: %~n0 today -12345
09. ::
10. :: Note that there is NO SPACE between the plus or minus sign and the
11. :: number of days.
12. ::
13. :: The result is returned in environment variable %~n0 in yyyymmdd format.
14. ::
15. :: A third (optional) argument of '.' will display the result on screen.
16. ::
17. if [%1] NEQ [] if [%1] NEQ [?] if [%1] NEQ [/?] if [%1] NEQ [-?] if /i [%1] NEQ [HELP] goto :begin
18. call XHELP %~f0
19. goto :EOF
20.
21. :begin
22. if not defined cd (echo.&echo Win2000 or WinXP only.&goto :EOF)
23. set mydate=%1
24. if /i "%mydate%" NEQ "TODAY" goto :YMD
25.
26. set mydate=%date:~4%
27. set mydate=%mydate:/=%
28. set mydate=%mydate:~4,4%%mydate:~0,4%
29.
30. :YMD
31.
32. set M=%mydate:~4,2%
33. set D=%mydate:~6,2%
34. set Y=%mydate:~0,4%
35. set TD=%D%X
36. set TM=%M%X
37. if %TD:~1,1%==X ( set D=10%D% ) else ( set D=1%D% )
38. if %TM:~1,1%==X ( set M=10%M% ) else ( set M=1%M% )
39. set /a D-=100,M-=100
40.
41. set operation=%2
42.
43. :: Generate a Magic number from %D% %M% %Y%
44. set /a X=(1461*(Y+4800+(M-14)/12))/4+(367*(M-2-12*^
45. ((M-14)/12)))/12-(3*((Y+4900+(M-14)/12)/100))/4+D-32075
46.
47. :: Do date arithmetic here, eg 'set /a X+=30'
48.
49. set /a X = X %operation%
50.
51. :: Convert Magic number back to a date
52. set /a L=X+68569
53. set /a N=(4*L)/146097
54. set /a L=L-(146097*N+3)/4
55. set /a K=(4000*(L+1))/1461001
56. set /a L=L-(1461*K)/4+31
57. set /a J=(80*L)/2447
58. set /a D=L-(2447*J)/80
59. set /a L=J/11
60. set /a M=J+2-(12*L)
61. set /a Y=100*(N-49)+K+L
62.
63. set /a D+=100,M+=100
64.
65. if [%3]==[.] echo %Y% %M:~1% %D:~1%
66. endlocal&set %~n0=%Y%%M:~1%%D:~1%&goto :EOF
=====end C:\CMD\TEST\datemath.cmd====================
=====begin C:\CMD\TEST\xhelp.cmd====================
01. @echo off
02. ::
03. :: Displays the contents of the 'flowerbox' in a CMD or BAT
04. :: file.
05. ::
06. :: '%~n0' will be displayed as the name of the referenced file
07. ::
08. echo .?./?.-?.help.. | findstr /i ".%1." > nul
09. if %errorlevel% EQU 0 (
10. call XHELP %~f0
11. goto :EOF
12. )
13. setlocal
14. set cd=
15. set whichfile=%1
16. for /f "tokens=*" %%a in (
17. 'dir %whichfile% ^| find "/"'
18. ) do set direntry=%%a
19. set fname=%~n1
20. echo :
21. echo : %direntry:~0,38% %whichfile%
22. echo :
23. for /f "tokens=* delims=" %%* in (
24. 'findstr /b /c:"::" %whichfile%') do call :display %%*
25. endlocal& goto :EOF
26. :display
27. set line=%*
28. set line=%line:~1%
29. if not defined cd set line=%line:~1%
30. call set line=%%line:^%~n0=%fname%%%
31. echo %line%
32. goto :EOF
33. :EOF
34.
=====end C:\CMD\TEST\xhelp.cmd====================

If you are using WinNT 4.0, please say so and I will post a CALCDATE
batch file that will do date calculations. Alternatively, you could
use WSH to do date calculations. What's your preference?

--
U s e ' R e p l y - T o ' a d d r e s s o r
u n z i p ' F r o m ' t o s e n d m a i l

Ritchie

unread,
Oct 29, 2002, 3:31:00 AM10/29/02
to
"Phil Robyn" <zipp...@uclink.berkeley.edu> wrote in message news:3DBE1639...@uclink.berkeley.edu...

> Ritchie previously posted an extremely nice DATEMATH batch file, which
> you could use to do this *if* you have Win2000 or WinXP.

Hello Phil,
That must have been the first datemath function I ever wrote (and before I
knew about NT's nuances). All the date and time functions I've written
since support NT4... in fact they're all online..........

http://www.commandline.co.uk/cmdfuncs/index.html

--
Ritchie
Undo address for mail


Phil Robyn

unread,
Oct 30, 2002, 12:51:16 AM10/30/02
to

Thanks, Ritchie! A truly stellar collection!

0 new messages