Saturday, January 29, 2011

Finding day of week in batch file? (Windows Server 2008)

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

  • 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

    Joey : 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.
    Zypher : If your going to use powershell at all you might as well translate the whole script to powershell.
    Joey : Zypher, that's the obvious idea, of course :-)
    Daniel Magliola : 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.
  • 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).

    Daniel Magliola : 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)
    From John T
  • 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...

    Daniel Magliola : 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...
    jscott : echo %DATE:~0,3%
    Daniel Magliola : jscott: like i mentioned in the question, date doesn't return the day of week for me. Only dd/mm/yyyy.
    From Phil Swiss
  • 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

    From RobW
  • 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%.

0 comments:

Post a Comment