Link to home
Start Free TrialLog in
Avatar of transpennine
transpennine

asked on

How to get a batch file to run on certain days of the week

Hello I have searched the internet and this site and found loads of examples of what I think i'm looking for but just can't get them to work! and really do need your help

What I need is that when the login.bat runs it checks to see which Day of the week it is and then run the batch file for that day the code I have below seems to work but doesn't return the day as monday or tuesday etc but seems to return a date as 08/07/09 it does this for each line
it is as if the code can't figure out the day and just defaults to the date! and as I need this batch file to work from the days of the week there my problem! hopefully someone here can have a go at helping!

Operating system windows xp clients and server 2003  

Deleted by Vee_Mod, no points refunded:  7/28/2009 3:36:58 PM
net use g: \\SERVER\SHARE
net use j: \\SERVER\SHARE
 
 
@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @( 
    Set DAY=%%A
    Set MONTH=%%B
    Set Year=%%C
)
 
@echo DAY = %Day%
@echo Month = %Month%
@echo Year = %Year%
 
IF %day% == monday GOTO Advert1
IF %day% == tuesday GOTO Advert2
IF %day% == wednesday GOTO Advert3
IF %day% == thursday GOTO Advert4
IF %day% == friday GOTO Advert5
 
:Advert1
echo Advert1
start j:\advert1.exe
goto EOF
 
:Advert2
echo Advert2
start j:\advert2.exe
goto EOF
 
:Advert3
echo Advert3
start j:\advert2.exe
goto EOF
 
:Advert4
echo Advert4
start j:\advert4.exe
goto EOF
 
:Advert5
echo Advert5
start j:\advert5.exe
goto EOF
 
:EOF
endlocal
 
wait

Open in new window

Avatar of Neil Russell
Neil Russell
Flag of United Kingdom of Great Britain and Northern Ireland image

See the following question for the answer

https://www.experts-exchange.com/questions/21356348/get-day-of-week-in-dos-batch-file.html

Days are Mon, Tue, Wed

Not monday, tuesday etc..

Hope that helps.

N.
Avatar of deroode
Depending on regional settings your DATE /T will return a complete day name, or an abreviated, or none at all...

What is the output of DATE /T when executed in a CMD prompt window?

AND: when a day is returned, it will be token 1. In your batch file you only pickup tokens 2,3,4 which are then day (1-31), month (1-12) and year

So the batch would become:



@For /F "tokens=1 delims=* " %%A in ('Date /t') do Set DAY=%%A
 
@echo DAY = %Day%
 
IF %day% == mon GOTO Advert1
IF %day% == tue GOTO Advert2
IF %day% == wed GOTO Advert3
IF %day% == thu GOTO Advert4
IF %day% == fri GOTO Advert5

Open in new window

The script you have is trying to pull out the day number, month number and year from dd/mm/yyyy etc.  If you type just

date /t

does it return the day of the week as part of the output.  Trouble is then depending upon the regional settings on a pc it may or not include the day, e.g.

Wed 08/07/2009 or just 08/07/2009

Best bet is to use a VB Script to get the day value unless this is on a known machine where the regional settings are known to be the same.  If you do want to use just batch and echo %date% from a cmd.exe prompt includes the day then you can use a version of what you have.

Save this line as weekday.vbs
wscript.echo weekdayname(weekday(date)) & "," & weekday(date) & "," & weekdayname(weekday(date),true)

Then in your script use

for /f "tokens=1,2,3 delims=," %%a in ('cscript //nologo weekday.vbs') do set weekday=%%a & set weekdaynum=%%b & set shortday=%%c
echo %weekday%
echo %weekdaynum%
echo %shortday%

hth

Steve
Sorry was a bit slow hitting submit there!
Avatar of transpennine
transpennine

ASKER

Hi and wow thanks for all the quick responces to my problem

when I type date/t it returns 08/07/2009 so my regional setting must be causing me the trouble
i will try steves VB script to see it that will work for me , but is there another way to keep it more simple using a dos command as I want if possible to keep the login script as simple as possible

Regards Terry
The current versions of windows don't return the weekday name as part of the date command, just the numeric date (e.g. 07/07/2009).
VBScript will do this easily though
Dim sDayName
Dim oShell
Set oShell = CreateObject("WScript.Shell")
 
sDayName = LCase(WeekdayName(Weekday(Now())))
wscript.echo "It's "&sDayName
Select Case sDayName
	Case "monday"
		oShell.Run "cmd.exe /c monday.cmd",1,True
	Case "tuesday"
		oShell.Run "cmd.exe /c tuesday.cmd",1,True
	Case "wednesday"
		oShell.Run "cmd.exe /c wednesday.cmd",1,True
	Case "thursday"
		oShell.Run "cmd.exe /c thursday.cmd",1,True
	Case "friday"
		oShell.Run "cmd.exe /c friday.cmd",1,True
	Case Else
		WScript.Echo "Day-specific activity not defined"
End Select

Open in new window

Thanks Steve and Robin for the VB scripts which both work and display on my screen what the current day is, which is great but how to I make the dos batch file understand that the day is what the VB script say it is and to use it , how do I call the vb sript from with in the bat file also I can't have the pop up box appear on each client at login! I know am asking a lot ,

just as a test I did change the "IF %day% == mon GOTO Advert1"
to = IF %day% == 08/09/2009 GOTO Advert1" and this worked so I'm sure the problem is just getting the bat file to understand the day veses date issiue

Regards Terry

my entry includes a for command to read the output of he vbs and set you three different variables you ca use to check, e.g.

if %shortdate%==Mon goto ...
You could use Bill Stewart's dateX utility:

for /f %%d in ('datex -f ddd') do set DOW=%%d

The datex utility is part of a set of tools Bill wrote to make batch scripting easier. You can get it here:

http://www.westmesatech.com/sst.html

(found on https://www.experts-exchange.com/questions/24447856/Getting-Day-of-Week-from-DOS.html)
use:
cscript.exe //nologo dayofweek.vbs

The VBscript will run within the current DOS box that your existing batch/cmd script is running in.

To make the monday.cmd/tuesday.cmd etc. not show you can change the ,1,True for ,0,True which will hide the resulting dos box for the secondary scripts.
Hi all i'm getting much closer now if I run the batch below it reports the day as wednesday 4 wed
which is what i need but then seems to ignore the if statement and just runs down and trys to start

:Advert1
echo Advert1
start j:\advert1.exe
goto EOF
 why is it irnoring the "if statement and "eod" routine what have i got wrong?

net use g: \\SERVER\SHARE
net use j: \\SERVER\SHARE


@echo off
setlocal

for /f "tokens=1,2,3 delims=," %%a in ('cscript //nologo weekday.vbs') do set weekday=%%a & set weekdaynum=%%b & set shortday=%%c
echo %weekday%
echo %weekdaynum%
echo %shortday%

pause

IF %shortday% == mon GOTO :Advert1
IF %shortday% == tue GOTO :Advert2
IF %shortday% == wed GOTO :Advert3
IF %shortday% == thu GOTO :Advert4
IF %shortday% == fri GOTO :Advert5


:Advert1
echo Advert1
start j:\advert1.exe
goto EOF

:Advert2
echo Advert2
start j:\advert2.exe
goto EOF

:Advert3
echo Advert3
start j:\advert3.exe
goto EOF

:Advert4
echo Advert4
start j:\advert4.exe
goto EOF

:Advert5
echo Advert5
start j:\advert5.exe
goto EOF

:EOF
endlocal

regards Terry
The day is Wed not wed and it is case sensitive.  Try using Mon Tue Wed etc.

You don't need the three echo lines btw, there just to show you it is working.  Likewise could amend the weekday.vbs to just include one, I just leave all three in for when they are more useful.

You COULD use the %weekdaynum% entry without the goto's as:

start j:\advert%weekdaynum%.exe if you numbered them to match the days no's returned (sunday is 1...)

Steve
i.e. rename your files advert2.exe through to advert6.exe and use the aboe instead of all the goto and if's

Steve
eXCELLENT WOW it's working thank you so much I just had the wrong case set in the day field this batch file will be so useful

Again a big thank you to all who helped it is very appricated

Regards Terry
No problem... don't forget to answer(s) and grade them.

Steve
which judging by all your questions are still open... click on your open questions here:

https://www.experts-exchange.com/M_4285539.html

and then open each Question and choose "select as answer" on each... that is the only way experts get rewarded by earning points and grades within EE....

Steve
Hi all one final snag I have

if I run from my local pc perfect
if i run the batch login.bat from the domain controller its fine
if I login to the domain it maps my network drives but then seems to ignore the rest of the code in the batch , but if I then open the j:\drive and run the batch manually it checks the day and fires up the correct advert so my question is why is the logon.bat file not doing the code and is there any way I can force the logon.bat to wait or step thought the logon.bat

I have given the domain controllers plenty of time to replicate between each other but still no joy

Regards Terry
It is probably that the .vbs script needs to be specified directly, i.e. \\server\netlogon\weekday.vbs. We can have the batch file make the weekday.vbs in your %temp% drive and run it from there.  Just off out now, will give you more later.

Steve
Try this.  Will create the vbs file on the fly in the users temp dir and then run that.  Alternative is put the vb somewhere already except in the logon script dir as by default the current directory isn't the logon script diectory (as it is \\server\sysvol type unc path) but the windows system dir so it can't find the vbs I guess.

echo wscript.echo weekdayname(weekday(date)) ^& "," ^& weekday(date) ^& "," ^& weekdayname(weekday(date),true) > "%temp%\weekday.vbs"
for /f "tokens=1,2,3 delims=," %%a in ('cscript //nologo "%temp%\weekday.vbs"') do set weekday=%%a & set weekdaynum=%%b & set shortday=%%c

ASKER CERTIFIED SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks again for this code I have now tested it on 5 user accounts across the domain and it seems to be working I will add a few more in, over the next few days and wait for all the complaints from our users about having these company adverts pop up each day! hehe but it's working great so far. our marketing team are going to be very happy

again thanks for all you help

Regards Tez
No problem.... remember to accept an answer .... and to your other two questions that are open.

Steve
thanks all for your help sorry I forgot to grade but I trying my best to do it now
Hi all I can't seem to get the system to except the grading what am I doing wrong??
Not sure.  You should have an "accept as answer" button or something showing next to each answer. (I can't see this obviously as it is your Q).  If it's not working properly for you try requesting assistance (there is a link at the top of the Q or click here:)

https://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/requestAttention.jsp?attention&qid=24552630

Steve
Thanks Steve I have used the link you providied and asked the question!

Regards Terry
hello all I have attempted to grade all these questions but I keep getting a javavoid error at the bottom of the web page! am very sorry that I haven't been able to award any points but it wasn;t from a lack on trying

Regards Tez