computing
  • 1

Extract Day Of The Week In a Dos Batch File

  • 1

I need to write a dos batch that works both in Winodws XP and 7 that displays the date when a file was last modified in the format (dow, dd-mm-yyyy). I succeeded to write the code that returns the date in the format dd-mm-yyyy, but I can’t find out how I can add the day of the week.
This is the code that I’m currently using, that returns e.g. “06/04/2012”

FOR %%a in (c:\DB\inventory.txt) DO SET fileDate=%%~ta
msg * File last modified on %fileDate:~0,10%

I’d like instead to have as output “Fri, 06/04/2012” or even better “Friday, 06/04/2012”
Any help is greatly appreciated!!!

Share

1 Answer

  1. Below is a script which is a slight rehash of work appearing in the Rob Van Der Woude stable. See http://www.robvanderwoude.com/datet… where all due credits appear.

    The script is untested and depends upon your Date format being dd/mm/yyyy

    @echo off
    cls
    setlocal enabledelayedexpansion
    
    FOR %%a in (c:\DB\inventory.txt) DO SET filedate=%%~ta
    
        set yy=%filedate:~7,4%
        set mm=%filedate:~4,2%
        set dd=%filedate:~0,2%
    
        IF %mm% lss 10 SET mm=%mm:~1%
        IF %dd% lss 10 SET dd=%dd:~1%
    
        SET /A mm1 = ( %mm% - 14 ) / 12
        SET /A yy1 = %yy% + 4800
        SET /A juldate  = 1461 * ( %yy1% + %mm1% ) / 4 + 367 * ( %mm% - 2 -12 * %mm1% ) / 12 - ( 3 * ( ( %yy1% + %mm1% + 100 ) / 100 ) ) / 4 + %dd% - 32075
    
        SET weekday.0=Monday
        SET weekday.1=Tuesday
        SET weekday.2=Wednesday
        SET weekday.3=Thursday
        SET weekday.4=Friday
        SET weekday.5=Saturday
        SET weekday.6=Sunday
    
        SET /A wd = %juldate% %% 7
    
        SET weekday=!weekday.%wd%!
    
        
    msg * File last modified on %weekday%, %fileDate:~0,10% 
    

    • 0