Quote:
Originally Posted by gurutech I have a batch file that runs every day, which is fine, but there are certain parts of it that I want to run on a certain date. If it's not that certain date, then skip over that part of the batch file.
I know how to use the "goto" command to redirect the batch file to the proper section, but how do I check the date in a batch file?
Thanks in advance! |
Hi there,
I could not help but notice your post.
I have some vbs code (requires WSCRIPT to be allowed) that sets a number op environment variables which you can use to create folders, logs, whatever, look into these maybe they can be of use.
Create a directory C:\BAT (or anything you wish and wherever you wish)
You will need to create the following files:
Setvars.vbs
GetEm.Bat
EnvShow.Bat
Alls codes to be found hereunder:
Setvars.vbs
dtUur = Right("0" & hour(time),2)
dtMin = Right("0" & Minute(time) ,2)
dtTijd = dtUur & ":" & dtMin
Years = year(date)
Months = Right("0" & datepart("m",date),2)
Days = Right("0" & datepart("d",date),2)
WeekNr = wNumber(year(date), month(date), day(date))
If Int(WeekNr / 2) = WeekNr /2 Then WeekOdd = "NO" Else WeekOdd = "YES"
Public Const cHideWindow = 0, cNormalWindow = 1
Set WSHShell = Wscript.CreateObject("WScript.Shell")
Set FSO = Wscript.CreateObject("Scripting.FileSystemObject")
Set EnvVar = wshShell.Environment("Process")
tQ1 = Kwartaal(date)
tQ2 = Kwartaal( date + 1)
tempFile= EnvVar("TEMP") & "\EnvVars.bat"
Set File2Write = fso.createtextfile(tempFile,1)
File2Write.writeline("Set Hour=" & DtUur )
File2Write.writeline("Set Time=" & dtUur & ":" & dtMin)
File2Write.writeline("set Year=" & Years)
File2Write.writeline("set Month=" & Months)
File2Write.writeline("set MonthDay=" & Months & Days)
File2Write.writeline("set YYYYMMDD=" & Years & Months & Days )
File2Write.writeline("set MMDDJJJJ=" & Months & "-" & Days & "-" & Years)
File2Write.writeline("set YearMonth=" & Years & Months)
File2Write.writeline("set Today=" & Days & "-" & Months & "-" & Years )
File2Write.writeline("set WeekNr="& WeekNr)
File2Write.writeline("set WeekOdd="& WeekOdd)
File2Write.writeline("set Quarter="& tQ2 - tQ1)
File2Write.writeline("set StartDate=" & right("0" & day(date-6),2) & "-" & right("0" & month(date-6),2) & "-" & year(date-6))
File2Write.writeline("set StopDate=" & right("0" & day(date-1),2) & "-" & right("0" & month(date-1),2) & "-" & year(date-1))
File2Write.close
tempFile= EnvVar("TEMP") & "\OK.txt"
Set File2Write = fso.createtextfile(tempFile,1)
File2Write.close
Wscript.Quit
function WNumber(Y, M, D)
dim N, Tdy, Thu, SoY, YNo, WNo, DoW
N = 2
Tdy = DateSerial(Y, M, D) '' Date, args
Thu = ((Tdy+3+N) \ 7) * 7 - N '' Nearest Thu
YNo = Year(Thu)
SoY = DateSerial(YNo, 1, 1) '' Date, YYYY-01-01
WNo = ((Thu - SoY) \ 7) + 1
WNumber = Right(100+WNo,2) '' always return two digits
end function
Function Kwartaal(tDate)
mnd = Month(tDate)
If mnd = 1 Or mnd = 2 Or mnd = 3 Then
Kw = 1
ElseIf mnd = 4 Or mnd = 5 Or mnd = 6 Then
Kw =2
ElseIf mnd = 7 Or mnd = 8 Or mnd = 9 Then
Kw = 3
Else
Kw = 4
End If
Kwartaal = Kw
End Function
GetEm.Bat
@echo off
rem This file calls SetVars.vbs, make sure the path exists in my case C:\Bat
rem First it deletes the files if they exist
rem Setvars.vbs creates two (2) files in the user's TEMP foldeer
rem OK.txt
rem EnvVars,bat
rem this batchfile loops until the %TEMP%\OK.txt is created
rem Then it calls EnvVars.bat to have the varaiables active for use
rem To view the values run EnvShow.bat
if exist %TEMP%\Ok.txt del %TEMP%\Ok.txt
if exist %TEMP%\EnvVars.Bat del %TEMP%\EnvVars.bat
:waitloop
Wscript C:\Bat\SetVars.vbs
if not exist %TEMP%\OK.txt goto waitloop
CALL %TEMP%\EnvVars.Bat
To see the results Wacht the C:\BAT path I use and make it your path
EnvShow.bat
@Echo off
call C:\Bat\GetEm.Bat
call %temp%\EnvVars.bat
echo %date% %time%
Echo HOUR=%hour%
Echo TIME=%Time%
Echo YEAR=%year%
Echo MONTH=%month%
Echo MONTHDAY=%monthday%
Echo YYYYMMDD=%yyyymmdd%
Echo YEARMONTH=%yearmonth%
Echo MMDDJJJJ=%MMDDJJJJ% format is mm dd yyyy
Echo TODAY=%today% format is dd mm yyyy
Echo WEEKNR=%WeekNr% Is Week ODD? WEEKODD=%WEEKODD%
Echo QUARTER=%quarter% (if value = 1 then tomorrow is a new Quarter)
Echo StartDate=%startdate%
Echo StopDate=%stopdate%
pause
Questions???
Just ask.