Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

DOS/Other DOS/Other
Search Search
Search for:
Tech Support Guy > > >

Batch file to only run on specified date


(!)

gurutech's Avatar
Computer Specs
Member with 2,911 posts.
THREAD STARTER
 
Join Date: Apr 2004
Location: Central NJ
Experience: Seasoned Professional
28-Mar-2008, 10:24 AM #1
Batch file to only run on specified date
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!
TheOutcaste's Avatar
Computer Specs
Member with 9,028 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
28-Mar-2008, 10:39 PM #2
%date% will give you the current date in a batch file. You can use an if statement to check it.
The format will be the same as your regional settings, so it depends on your settings and what exactly you want to check, say skip a part every saturday/sunday, or skip on the 5th, 10th, 15th, etc.
Couple of ways to break down the data, either use set (see set /?) to extract a substring (can be iffy if your system doesn't always use 2 digits for month/day), or a FOR statment with the proper delimiters
For example on my system %date% is Fri 03/28/2008, so:
Set _day=%date:~0,3% would set _day to Fri
set _date=%date:~7,2% would set _date to 28
If your settings don't display a leading zero, then this last one won't work consistantly because the position in the string changes. Better to use FOR in that case.

This FOR statement will parse the %date% value using a space and / as delimiters, and puts all 4 values into separate variables:
for /f "tokens=1-4 delims=/ " %%I in ("%date%") do Set _Day=%%I & Set _Month=%%J & Set _Date=%%K & Set _Year=%%L
If you just need one value (say the date), change the tokens=1-4 to tokens=3 and just use set _date=%%I.
If your settings don't display the day of the week, remove the space between the / and " after delims=

See for /? for info on those options.

HTH

jerry
gurutech's Avatar
Computer Specs
Member with 2,911 posts.
THREAD STARTER
 
Join Date: Apr 2004
Location: Central NJ
Experience: Seasoned Professional
30-Mar-2008, 10:56 PM #3
Thanks Jerry - I was actually able to do this with just a line:

if "%date%!" == "Tue 04/01/2008!" goto PRANK

(guess what I'm doing to my boss on Tues... LOL)
Keebellah's Avatar
Keebellah   (Hans) Keebellah is offline Keebellah is a Trusted Advisor with special permissions. Keebellah has a Profile Picture
Computer Specs
Trusted Advisor with 5,268 posts.
 
Join Date: Mar 2008
Location: Oegstgeest, The Netherlands
Experience: Advanced
02-Apr-2008, 09:59 AM #4
Smile Good Old Dos
Quote:
Originally Posted by gurutech View Post
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.
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
WELCOME TO TECH SUPPORT GUY!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.


(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑