Setting a windows batch file variable to the day of the week

后端 未结 17 1891
南方客
南方客 2020-11-27 20:14

I have a windows batch file that runs daily. Wish to log data into a file and want to rotate it (i.e. having at most the last 7 days worth of data).

Looked into the

相关标签:
17条回答
  • 2020-11-27 20:34
    @ECHO OFF
    REM GET DAY OF WEEK VIA DATE TO JULIAN DAY NUMBER CONVERSION
    REM ANTONIO PEREZ AYALA
    REM GET MONTH, DAY, YEAR VALUES AND ELIMINATE LEFT ZEROS
    FOR /F "TOKENS=1-3 DELIMS=/" %%A IN ("%DATE%") DO SET /A MM=10%%A %% 100, DD=10%%B %% 100, YY=%%C
    REM CALCULATE JULIAN DAY NUMBER, THEN DAY OF WEEK
    IF %MM% LSS 3 SET /A MM+=12, YY-=1
    SET /A A=YY/100, B=A/4, C=2-A+B, E=36525*(YY+4716)/100, F=306*(MM+1)/10, JDN=C+DD+E+F-1524
    SET /A DOW=(JDN+1)%%7
    

    DOW is 0 for Sunday, 1 for Monday, etc.

    0 讨论(0)
  • 2020-11-27 20:34

    If you can change format of short date in the PC to "ffffd yyyy-MM-dd" (only first parameter 'ffffd' is compulsory), then following command returns-

    c:\>vol | date
    The current date is: Mon 2014-12-01
    

    Then you can write you batch file -

    @echo off
    
    vol | date | find /i "sun" > nul 
    if not errorlevel 1 goto SUN
    
    vol | date | find /i "mon" > nul 
    if not errorlevel 1 goto MON
    
          # write block for other week days    
    
    goto END
    
    :SUN
    set fname="sun"
    goto BACKUP
    
    :MON
    set fname="mon"
    goto BACKUP
    
          # write block for other week days
    
    :BACKUP
    echo %fname%
    
    :END
    
    0 讨论(0)
  • 2020-11-27 20:35

    This turned out way more complex then I first suspected, and I guess that's what intrigued me, I searched every where and all the methods given wouldnt work on Windows 7.

    So I have an alternate solution which uses a Visual Basic Script.

    The batch creates and executes the script(DayOfWeek.vbs), assigns the scripts output (Monday, Tuesday etc) to a variable (dow), the variable is then checked and another variable (dpwnum) assigned with the days number, afterwards the VBS is deleted hope it helps:

    @echo off
    
    REM Create VBS that will get day of week in same directory as batch
    echo wscript.echo WeekdayName(Weekday(Date))>>DayOfWeek.vbs
    
    REM Cycle through output to get day of week i.e monday,tuesday etc
    for /f "delims=" %%a in ('cscript /nologo DayOfWeek.vbs') do @set dow=%%a
    
    REM delete vbs
    del DayOfWeek.vbs
    
    REM Used for testing outputs days name
    echo %dow%
    
    REM Case of the days name is important must have a capital letter at start
    REM Check days name and assign value depending
    IF %dow%==Monday set downum=0
    IF %dow%==Tuesday set downum=1
    IF %dow%==Wednesday set downum=2
    IF %dow%==Thursday set downum=3
    IF %dow%==Friday set downum=4
    IF %dow%==Saturday set downum=5
    IF %dow%==Sunday set downum=6
    
    REM print the days number 0-mon,1-tue ... 6-sun
    echo %downum%
    
    REM set a file name using day of week number
    set myfile=%downum%.bak
    
    echo %myfile%
    
    pause
    exit
    

    EDIT:

    Though I turned to VBS, It can be done in pure batch, took me a while to get it working and a lot of searching lol, but this seems to work:

     @echo off
    SETLOCAL enabledelayedexpansion
    SET /a count=0
    FOR /F "skip=1" %%D IN ('wmic path win32_localtime get dayofweek') DO (
        if "!count!" GTR "0" GOTO next
        set dow=%%D
        SET /a count+=1
    )
    :next
    echo %dow%
    pause
    

    The only caveat for you on the above batch is that its day of weeks are from 1-7 and not 0-6

    0 讨论(0)
  • 2020-11-27 20:35

    This is not my work (well, I modified it slightly from the example), and it's late to the game, but this works on Server 2003 for me;

    @echo off  
    set daysofweek=Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday  
    for /F "skip=2 tokens=2-4 delims=," %%A in ('WMIC Path Win32_LocalTime Get DayOfWeek /Format:csv') do set daynumber=%%A  
    for /F "tokens=%daynumber% delims=," %%B in ("%daysofweek%") do set day=%%B
    

    Citation: TechSupportForum

    0 讨论(0)
  • 2020-11-27 20:36

    %DATE% is not your friend. Because the %DATE% environment variable (and the DATE command) returns the current date using the Windows short date format that is fully and endlessly customizable. One user may configure the system to return 07/06/2012 while another might choose Fri060712. Using %DATE% is a complete nightmare for a BAT programmer.

    There are two possible approaches to solve this problem:

    1. You may be tempted to temporarily change the short date format, by changing the locale settings in the registry value HKCU\Control Panel\International\sShortDate, to your recognizable format. Then access %DATE% to get the date in the format you want; and finally restore the format back to the original user format. Something like this

      reg copy "HKCU\Control Panel\International" "HKCU\Control Panel\International-Temp" /f >nul
      reg add "HKCU\Control Panel\International" /v sShortDate /d "ffffd" /f >nul
      set DOW=%DATE%
      reg copy "HKCU\Control Panel\International-Temp" "HKCU\Control Panel\International" /f >nul
      

      but this method has two problems:

      • it tampers with a global registry value for its local particular purpouses, so it may interfere with other processes or user tasks that at the very same time query the date in short date format, including itself if run simultaneously.

      • and it returns the three letter day of the week in the local language that may be different in different systems or different users.

    2. use WMIC Win32_LocalTime, that returns the date in a convenient way to directly parse it with a FOR command.

      FOR /F "skip=1" %%A IN ('WMIC Path Win32_LocalTime Get DayOfWeek' ) DO (
        set DOW=%%A
      )
      

      this is the method I recommend.

    0 讨论(0)
  • 2020-11-27 20:37

    This works for me

    FOR /F "tokens=3" %%a in ('robocopy ^|find "Started"') DO SET TODAY=%%a
    
    0 讨论(0)
提交回复
热议问题