Solved

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

Posted on 2009-07-08
32
2,975 Views
Last Modified: 2013-12-27
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

0
Comment
Question by:transpennine
  • 13
  • 10
  • 2
  • +2
32 Comments
 
LVL 37

Expert Comment

by:Neil Russell
Comment Utility
See the following question for the answer

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_21356348.html

Days are Mon, Tue, Wed

Not monday, tuesday etc..

Hope that helps.

N.
0
 
LVL 19

Expert Comment

by:deroode
Comment Utility
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

0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
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
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
Sorry was a bit slow hitting submit there!
0
 

Author Comment

by:transpennine
Comment Utility
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
0
 
LVL 14

Expert Comment

by:robincm
Comment Utility
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

0
 

Author Comment

by:transpennine
Comment Utility
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

0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
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 ...
0
 
LVL 19

Expert Comment

by:deroode
Comment Utility
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 http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/Windows/XP/Q_24447856.html)
0
 
LVL 14

Expert Comment

by:robincm
Comment Utility
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.
0
 

Author Comment

by:transpennine
Comment Utility
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
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
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
0
 
LVL 43

Expert Comment

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

Steve
0
 

Author Comment

by:transpennine
Comment Utility
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
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
No problem... don't forget to answer(s) and grade them.

Steve
0
 
LVL 43

Expert Comment

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

http://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
0
 

Author Comment

by:transpennine
Comment Utility
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
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
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
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
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

0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
Comment Utility
Have written a quick article on EE extending this to a few other date facilities in batch files:

http://www.experts-exchange.com/articles/OS/Microsoft_Operating_Systems/MS_DOS/Using-dates-in-batch-files-scripts.html

Steve
0
 

Author Comment

by:transpennine
Comment Utility
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
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
No problem.... remember to accept an answer .... and to your other two questions that are open.

Steve
0
 

Author Comment

by:transpennine
Comment Utility
thanks all for your help sorry I forgot to grade but I trying my best to do it now
0
 

Author Comment

by:transpennine
Comment Utility
Hi all I can't seem to get the system to except the grading what am I doing wrong??
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
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:)

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

Steve
0
 

Author Comment

by:transpennine
Comment Utility
Thanks Steve I have used the link you providied and asked the question!

Regards Terry
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
ok,  I would suggest my comment please:

The last comment from dragon-it is: http:#a24806590

http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_24552630.html?cid=238#a24806590
 
thanks

Steve
0
 

Author Comment

by:transpennine
Comment Utility
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
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now