DateExpanded Date Variables all in one package

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SaveMeGoogle
Posts: 2
Joined: 30 Aug 2019 12:19

DateExpanded Date Variables all in one package

#1 Post by SaveMeGoogle » 30 Aug 2019 13:00

I had need of a single file that I could CALL on to take care of all my dating needs in batch files for automation.

So with the help of google and quite a bit from DosTips I present the current version of this code.

There are several required files needed for this to run most of them came from here, and I thank each and every one of you that made this possible.

All files should be saved to C:\Procedures\DateTimeCommands\ or whatever variation you like once you make edits.

There are many variables available for getting different results and this is how you would call on them to get the information into your script. Or see DateExpandedTest.bat in the attachments

--------------------------------
::Enter this into your batch file which will call on DateExpanded.cmd
:: SubtractDay Set to NONE when not needed, Set to any number to generate a day previous of today based on the number you enter such as entering 1 would generate yesterday's date
:: SearchDay Set to NONE when not needed, Set to Sunday Monday Tuesday Wednesday Thursday Friday Saturday to pull the date for a day of the week so if today was Thursday and you want Monday's date enter Monday
:: ExactDay Set to NONE when not needed, Set to a two diget day of the month that you are looking for, so if you wanted the 7th of the current month enter 07
:: OVERRIDE Set to NONE when not needed, Set to the number of days you want to go back in time, Say today is the 15th and you want your script to pretend it was the 13th you would enter 2 and all functions will revolve around that day

SET SubtractDay=NONE
SET SearchDay=NONE
SET ExactDay=NONE
SET OVERRIDE=NONE
CALL C:\Procedures\DateTimeCommands\DateExpanded.cmd
------------------------------

These are the values you can call on once it runs and you can add other combinations of Year Month Day if you like
yyyy=year mm=month dd=day day=DayOfWeek jdate=JulianDate
Current is the current date unless the override variable is set which would affect most other dates below especially if it pushes the date to the previous month
Previous is todays date minus the SubtractDay variable
Search is the date of the search day entered
First is the first day of the current month
Last is the last day of the current month
Exact is the exact day of the current month

REM Variables available
REM Current - yyyy mm dd day YearMoDa MoDaYear jdate
REM Previous - pyyyy pmm pdd pday pYearMoDa pMoDaYear pjdate
REM Search --- syyyy smm sdd sday sYearMoDa sMoDaYear sjdate
REM FirstDay - fyyyy fmm fdd fday fYearMoDa fMoDaYear fjdate
REM LastDay - lyyyy lmm ldd lday lYearMoDa lMoDaYear ljdate
REM ExactDay - eyyyy emm edd eday eYearMoDa eMoDaYear ejdate

So if I wanted to check on a file I need to move for today I could do
IF EXIST C:\FTP_Pickup\%YearMoDa%_ImportantData.txt GOTO :Download
or maybe if I wanted yesterday's file and I had set SubtractDay=1
IF EXIST C:\FTP_Pickup\%pYearMoDa%_ImportantData.txt GOTO :Download
or today is Friday and the file always has Mondays date I could set SearchDay=Monday
IF EXIST C:\FTP_Pickup\%sYearMoDa%_ImportantData.txt GOTO :Download

Files attached all should be put into C:\Procedures\DateTimeCommands\ the zip should already be setup just extract to C
DateExpanded.cmd - Main call on script
DateExpandedTest.bat - Used to test and view various variable settings (includes already set items for SubtractDay SearchDay and ExactDay to show off)
GDate.cmd - Converts Julian days to Gregorian date format (I did not make this)
GetDate.cmd - Pulls current date (I did not make this)
jDate.cmd - Converts Gegorian Date Format to Julian Days (I did not make this)
GetTime.cmd - Gets the current time (I did not make this)
Procedures.zip
DateExpanded Script and sub files
(5.69 KiB) Downloaded 29 times
I am still in the process of updating our automation and will likely expand this script as needed. Such as maybe the Previous Months First and Last day.

aGerman
Expert
Posts: 3761
Joined: 22 Jan 2010 18:01
Location: Germany

Re: DateExpanded Date Variables all in one package

#2 Post by aGerman » 30 Aug 2019 14:36

You shouldn't use absolute paths in your scripts. And the GetDate.cmd won't work in a lot of environments around the world. Doesn't work for me btw. The output of time /t is in format DD.MM.YYYY here. Search the forum, we already had lengthy discussions about it.

Steffen

SaveMeGoogle
Posts: 2
Joined: 30 Aug 2019 12:19

Re: DateExpanded Date Variables all in one package

#3 Post by SaveMeGoogle » 30 Aug 2019 18:15

Ahh well that sucks, maybe it will only work in my environment. I took over automation 2 months ago when someone left. Before that the only thing I did was batches for ResEdit and mapping drives. I found the server a complete mess and the scripts in worse condition so they gave me a new server and I created a 30 page batch template to take care of any kind of action we need to take in processing files. We go through about 300,000 files a month with 120 scripts and I have 10 of the most problematic ones converted to the new server and finally stable. They all call on this to pull the dates they need to grab the right files. I’ll see what I can do to make it more universal.

aGerman
Expert
Posts: 3761
Joined: 22 Jan 2010 18:01
Location: Germany

Re: DateExpanded Date Variables all in one package

#4 Post by aGerman » 31 Aug 2019 07:21

Maybe a few functions out of my collection are helpful for you ...

Code: Select all

@echo off &setlocal EnableDelayedExpansion
for %%i in ("Sunday 0" "Monday 1" "Tuesday 2" "Wednesday 3" "Thursday 4" "Friday 5" "Saturday 6") do for /f "tokens=1*" %%j in (%%i) do set "%%j=%%k"&set "Long%%k=%%j"
for /f %%i in ('WMIC PATH Win32_LocalTime GET Year^,Month^,Day^,Hour^,Minute^,Second^,DayOfWeek /value') do for /f %%j in ("%%i") do set "%%j"

echo today                     %Year% %Month% %Day%
echo now                       %Hour% %Minute% %Second%
echo day of week               %DayOfWeek%
echo long day of week          !Long%DayOfWeek%!

call :MakeDateStamp Year Month Day DateStamp
echo date stamp                %DateStamp%

call :MakeTimeStamp Hour Minute Second TimeStamp
echo time stamp                %TimeStamp%

call :DaysInMonth Year Month DiM
echo days in month             %DiM%

call :DaysAdd Year Month Day -1 yYear yMonth yDay
echo yesterday                 %yYear% %yMonth% %yDay%

call :DaysAdd Year Month Day 1 tYear tMonth tDay
echo tomorrow                  %tYear% %tMonth% %tDay%

call :DayOfWeek tYear tMonth tDay DoW
echo day of week tomorrow      %DoW%
echo long day of week tomorrow !Long%DoW%!

echo day of week of Wednesday  %Wednesday%

pause
goto :eof


:MakeDateStamp  YearIn  MonthIn  DayIn  ByRef_DateStampOut
setlocal DisableDelayedExpansion
set /a "y=%~1,m=100+%~2,d=100+%~3"
endlocal &set "%~4=%y%%m:~-2%%d:~-2%" &goto :eof

:MakeTimeStamp  HourIn  MinuteIn  SecondIn  ByRef_TimeStampOut
setlocal DisableDelayedExpansion
set /a "h=100+%~1,m=100+%~2,s=100+%~3"
endlocal &set "%~4=%h:~-2%%m:~-2%%s:~-2%" &goto :eof

:DaysInMonth  YearIn  MonthIn  ByRef_DaysOut
setlocal DisableDelayedExpansion
set /a "n=30+((%~2+(%~2>>3))&1)+!(%~2-2)*((!(%~1&3)&(!!(%~1%%25)|!(%~1&15)))-2)"
endlocal &set "%~3=%n%" &goto :eof

:DaysAdd  YearIn  MonthIn  DayIn  DeltaIn  ByRef_YearOut  ByRef_MonthOut  ByRef_DayOut
setlocal DisableDelayedExpansion
set /a "a=(14-%~2)/12,b=%~1+4800-a,c=(153*(%~2+12*a-3)+2)/5+%~3+b*365+b/4-b/100+b/400-1+%~4,d=(4*c+3)/146097,e=-d*146097/4+c,f=(4*e+3)/1461,g=(-1461*f)/4+e,h=(5*g+2)/153"
set /a "y=d*100+f-4800+h/10,m=(-h/10)*12+h+3,d=((153*h+2)/-5)+g+1"
endlocal &set "%~5=%y%" &set "%~6=%m%" &set "%~7=%d%" &goto :eof

:DayOfWeek  YearIn  MonthIn  DayIn  ByRef_DoWOut
setlocal DisableDelayedExpansion
set /a "z=(14-%~2)/12,y=%~1+4800-z,DoW=((153*(%~2+12*z-3)+2)/5+%~3+y*365+y/4-y/100+y/400-2472636)%%7"
endlocal &set "%~4=%DoW%" &goto :eof
Note that the functions expect numbers without preceding 0. Otherwise they will be treated as octal numbers (where 08 and 09 would be invalid).

Steffen

Post Reply