Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a process I run from a batch file, and i only want to run it on a certain day of the week.
Is it possible to get the day of week?

All the example I found, somehow rely on "date /t" to return "Friday, 12/11/2009", however, in my machine, "date /t" returns "12/11/2009". No weekday there.

I've already checked the "regional settings" for my machine, and the long date format does include the weekday. The short date format doesn't, but i'd really rather not change that, since it'll affect a bunch of stuff I do.

Any ideas here?

Thanks!
Daniel

share|improve this question

migrated from superuser.com Dec 16 '09 at 6:14

This question came from our site for computer enthusiasts and power users.

    
better on SF``? –  Larry Dec 16 '09 at 6:06

13 Answers 13

I'm a little late to this party, but this will give you the day of week:

wmic path win32_localtime get dayofweek

Use a batch for loop to obtain the output, and you have a testable condition for the day of the week.

Rob

share|improve this answer

Batch can't do this easily. It requires a lot of command-line fu with the date command. There are a few ways to achieve what you're after, but most of all I'm wondering why the built-in Task Scheduler isn't an option? It allows you specify the days you want it to run and what time:

alt text

You could also use VBScript:

wscript.stdout.writeline weekdayname(weekday(date))

Then run it:

C:\Documents and Settings\Administrator>cscript /nologo dayofweek.vbs
Wednesday

You can easily use if/else logic in VBScript then have your VBScript code execute batch files depending on the day, and vice-versa (have batch call a VBScript).

share|improve this answer
    
I basically have A batch file that does a bunch of stuff (backups), and some of that stuff (HUGE folders that are not that important and don't change often), I want to only do it once a week to save hours of daily time. I CAN split the batch file into many and use scheduler to control this, but it makes everything much more complicated for me, since the things that I need to do weekly happen after some and also before some of the daily ones (in particular, I burn a DVD every day after the backup is done, and that has to happen AFTER the long running tasks are done (which is hard to estimate) –  Daniel Magliola Dec 16 '09 at 15:54
    
A very workable solution can be to have the task scheduler create a flag file on the desired day(s). The main batch file can check for the existence of that flag file and change the processing accordingly. Don't forget to delete the flag file as well. –  John Gardeniers Jan 31 '11 at 17:35

Here's a batch file that will get all the date and time info into variables but it does depend on you having an appropriate date format set on your machine. Also, the order might need adjusting depending on the local date format, I'm not sure.

@echo off
for /F "tokens=1-4 delims=/ " %%i in ('date /t') do (
set WD=%%i
set D=%%j
set M=%%k
set Y=%%l
) 

for /F "tokens=1-4 delims=: " %%i in ('time /t') do (
set H=%%i
set Mn=%%j
set AP=%%k
)

if "%AP%"=="PM" set /A H=%H%+12

echo %WD% %D%/%M%/%Y% - %H%:%Mn%

The bit you want is in %WD%.

share|improve this answer
    
I've used similar to this in a Batch file that ran daily to identify the day and only run a line of code on a specific day. –  HaydnWVN Nov 19 '12 at 9:33

May I quote something I found on the net:

About the DOW problem...

Win 2000 displays the current date by heading with the Day of Week, but Win XP gives you the bare date in your country format without DOW. This is one of the little annoiances affecting the two (almost) brothers operating sysyems.

To work around the trick is to force the legacy format displayed by (emulated) DOS

Echo.|Command /C Date|Find "current"

This really works, now you just have to fumble the DOW out of the outputstring and you're done...

share|improve this answer
    
I actually found that, tried it, and got the error that "command" doesn't exist... I actually found a bunch of solutions for XP that are not working in Server 2008... –  Daniel Magliola Dec 16 '09 at 15:57
    
echo %DATE:~0,3% –  jscott Dec 16 '09 at 17:40
    
jscott: like i mentioned in the question, date doesn't return the day of week for me. Only dd/mm/yyyy. –  Daniel Magliola Dec 16 '09 at 20:39

Incase anyone needs it, this works for for me on Windows 7 and XP...

echo.|command /C date|find /i "current" >%temp%\dow.txt
for /f "tokens=1-4 delims=/ " %%i in (%temp%\dow.txt) do set DOW=%%l
echo %DOW%

For Windows Server 2008 I'd go with one of the Powershell methods suggested above.

share|improve this answer

To avoid having to redirect to a text file, there is a variation of the FOR which takes a command string. Here it is:

@For /F "tokens=4 delims=/- " %i In ( '@Echo.^|Command /C Date^|Find /i "current"' ) Do @Echo %i

Remember to use double percent signs if you put this command in a dos script. In my example, I checked for the common date delimiter character of forward slash or dash.

I prefer this solution as it is solely DOS without having to rely on whether WMI, PowerShell, etc being present or not.

share|improve this answer

How about this one for Windows 2008, much more elegant. Also will work anywhere powershell is installed.

@for /F "tokens=1 delims=, " %%i In ('powershell date') do set dow=%%i
echo %dow%
share|improve this answer

Thanks to Phil Swiss previous posting - this works a treat :

echo. | cmd /C date | find "current"

Examples usage:

echo. | cmd /C date | find /i "Fri" && echo today is Friday

Or to reverse:

echo. | cmd /C date | find /i "Fri" || echo today is NOT Friday

share|improve this answer
    
The output from date depends on the Windows datetime settings so this may not work in some cases. For example on the server I'm currently working with the data command returns date as 2013-09-03. –  Juha Palomäki Sep 3 '13 at 20:28
@echo off

:set the variables
for /f %%C in ('wmic path Win32_LocalTime Get Year^,Month^,Day^,Hour^,Minute^,Second /Format:List 2^>nul ^| find "="') do @set current%%C

:display the variables
set current

Now all that remains is to pre-pend zeros to the day and month if either of them are less than 10.

share|improve this answer

Actually the flag for this (2K8, 2K3, XP etc) is the SHORT date format.

Under Regional and Language options - click Customize button On the DATE tab Change the SHORT Date format to "M/d/yyyy" Change the LONG Date format to "dddd, MMMM dd, yyyy"

Then the Day of the week shows up - to test this execute this in a dos prompt: date /t The output should be like this "Mon 01/31/2011"

share|improve this answer

The suggested change to regional settings will do the trick, but it's not available if your regional setting is Canada... it works as described when region is set to US... and I wouldn't change it on a server for fear that something else will crap out.

share|improve this answer

I am in the US. I can run this code in Windows 7, Windows 2008 R2, Windows Server 2003, Windows XP (All OS's are current with Windows Updates and patches). All with short date setting without ddd (or dddd) (day of week).

@echo off
for /f %%a in ('date /t') do set DAY=%%a
echo.
echo The Day Is: %DAY%
echo.

If today is Thursday, it would output "The Day Is: Thu".

This returns the day on all 4 Windows versions I have tested on. And only the day. When I changed my short date setup to be "ddd, M/d/yyyy", my output would show the day with a comma (e.g. Thu,) which tells me this code does use the short date format. But what also may be happening is that if the short date does not contain the day of week, it may look to the long date format which on all 4 machines I tested on, have dddd in the format.

share|improve this answer
    
The fact that this works on your machine doesn't really answer the OP's question. –  Katherine Villyard Feb 6 at 19:11
    
I've run this on 4 different machines. Each with a different OS. One being a Desktop PC (Windows 7), Server 2003 on a Server, Server 2008 and XP on VMs. I just am pointing out that it works for me in all 4 environments on 4 different machines. –  Scott Gehret Feb 6 at 21:15
up vote -1 down vote accepted

Ok, found an answer...
I couldn't find a way using plain BAT files, but if you have access to powershell...

@echo off
powershell date > date.txt
find/i "Fri" date.txt  > nul
if errorlevel 1 goto SkipWeeklyTasks

Do your weekly stuff here

:SkipWeeklyTasks
del date.txt

Hope it helps...

EDIT: (for potential people stumbling on this) This is nasty and ugly, and shouldn't be done, and other answers to this question are better, if they apply to you. If you NEED to get this done quickly and dirty within a batch file you already have, this could work for you, though.

Daniel

share|improve this answer
1  
Urgh. Please. If you have to use PowerShell for this, then at least give back a result that's (a) independent of any locale settings [the day of week need not be present and it doesn't have to be in English] and (b) is easily parseable. So powershell "&{[int](Get-Date).DayOfWeek}" is probably the better answer and you can easily put that into an environment variable and compare it. –  Joey Dec 16 '09 at 6:50
3  
If your going to use powershell at all you might as well translate the whole script to powershell. –  Zypher Dec 16 '09 at 7:11
    
Zypher, that's the obvious idea, of course :-) –  Joey Dec 16 '09 at 7:44
    
Johannes: Yah, this is actually a problem I need to solve and get rid of as quickly as possible. It only needs to work in MY machine(s). Every extra second I spend making this more generic than I need is esentially wasted. Zypher: Yah, but I already had my batch file running and working, and I don't know much about PowerShell, I only wanted to add a section that only ran once a week, and again, get rid of it as quickly as possible. But what you're both saying IS definitely good advice in case someone reads this solution. –  Daniel Magliola Dec 16 '09 at 15:56
    
If I remember an old script of mine I actually ended up with a VBScript file that returned the date to the BAT file. –  Brent Pabst Jul 3 '12 at 20:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.