tags:

views:

1301

answers:

5

hello i got a batch file, something like this:

if %day%==monday, tuesday, wednesday, thursday, friday (
goto yes
) else (
goto no
)

Now i know the first line won't work.

What i actually want to happen:

It automatticly checks which day it is. If it is Monday to Friday, it has to go to 'yes', otherwise (saturday/sunday) to 'no'.

How to do this?

+1  A: 
IF %day% == monday GOTO YES
IF %day% == tuesday GOTO YES
IF %day% == wednesday GOTO YES
IF %day% == thursday GOTO YES
IF %day% == friday GOTO YES
GOTO NO
Randolpho
does it really do it by itself,, or have the user to type it in?
YourComputerHelpZ
A: 

I don't have the batch fu to answer the question as asked, but, assuming this is a Windows batch file, consider executing the script using the task scheduler, which will let you set the kind of schedule you're asking for.

Michael Petrotta
i understand you,, but then i also have to give a specific time. the BATCH file will just run at startup.
YourComputerHelpZ
+1  A: 

I ran across this online. Tested, and it works. Returns the day as an integer, which you can still work with.

@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @( 
    Set Month=%%A
    Set Day=%%B
    Set Year=%%C
)

@echo DAY = %Day%
@echo Month = %Month%
@echo Year = %Year%
Michael Petrotta
Note that this won't work on locales where the date separator is not /
laalto
This will not be a reliable solution, as when I use 'date /t' here, all I get is the short one (YYYY-MM-DD), thus no day name. Goes with the locales. If this is for exportation (anyone not being you), I wouldn't bet on this working as intended.
Jay
+1  A: 

Here is an example bat file that will do this sort of thing I am sure you can think of other ways to use this sample code. For instance anytime you need an "in" list. The tricky bit is the %date:~0,3% this says expand the %date% environment variable and starting at position 0 the beginning of the string return the next 3 characters. You can learn more about this from the "set /?" command.

example: IsWeekDay.bat

@echo off
setlocal

for %%i in (Mon,Tue,Wed,Thu,Fri) do (
    if "%date:~0,3%"=="%%i" goto YES
)

:NO
echo No
goto EOF

:YES
echo Yes

:EOF
endlocal
Jeremy E
hmm... this doesn't seem to work for me.
YourComputerHelpZ
+1 Excellent. It works on Window7 for me. @YourComputerHelpZ can you %date% from the command line and also date /t. Can you tell us what you get.
Preet Sangha
A: 

As Jay has mentioned, using date /t will only work on locales where this command outputs the day of week along with the date, and won't work on other locales (e.g. Russian). If you don't mind mixing your batch files with some VBScript, here's a solution that should work on all locales.

The trick is this tiny VBScript script that outputs the day of the week as a number (1 = Sunday, 2 = Monday, ... 7 = Saturday):

WScript.Echo DatePart("w", Date)

You can run this script from your batch file, read its output and apply your logic:

for /f %%d in ('cscript dayofweek.vbs //nologo') do (
  if %%d==7 goto no   :: Saturday
  if %%d==1 goto no   :: Sunday
)
goto yes
Helen