Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2009-07-08
32
Medium Priority
?
3,820 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
28 Comments
 
LVL 37

Expert Comment

by:Neil Russell
ID: 24802380
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
ID: 24802407
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
ID: 24802443
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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 43

Expert Comment

by:Steve Knight
ID: 24802447
Sorry was a bit slow hitting submit there!
0
 

Author Comment

by:transpennine
ID: 24802510
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:Robin CM
ID: 24802525
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
ID: 24802781
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
ID: 24802853
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
ID: 24802861
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:Robin CM
ID: 24802992
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
ID: 24803318
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
ID: 24803631
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
ID: 24803649
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
ID: 24803884
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
 
LVL 43

Expert Comment

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

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 24804624
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
ID: 24805136
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
ID: 24805302
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
ID: 24806590
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 2000 total points
ID: 24811844
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
ID: 24811977
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
ID: 24812057
No problem.... remember to accept an answer .... and to your other two questions that are open.

Steve
0
 

Author Comment

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

Author Comment

by:transpennine
ID: 24821330
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
ID: 24821363
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
ID: 24821392
Thanks Steve I have used the link you providied and asked the question!

Regards Terry
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 24987705
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
ID: 25059907
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses
Course of the Month5 days, 22 hours left to enroll

772 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