Search code examples
batch-filecommand-lineexport-to-csv

Batch file: Export data in a CSV file


I have the following batch file that exports the date from the calendar starting from tomorrow from the day I run the batch file and continuing as many days as I need, I export them in a csv file. at the moment the script adds one day to the days. The problem is that I don't know how to make it go to the next month. when it reaches day 31, continue with 32, 33, 34.

@Echo off
 
WMIC.EXE Alias /? >NUL 2>&1 || GOTO s_error

 
FOR /F "skip=1 tokens=1-6" %%G IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
   IF "%%~L"=="" goto s_done
      Set _yyyy=%%L
      Set _mm=00%%J
      Set _dd=00%%G
      Set _hour=00%%H
      SET _minute=00%%I
)
:s_done

:: Pad digits with leading zeros
      Set _mm=%_mm:~-2%
      Set _dd=%_dd:~-2%
      Set _hour=%_hour:~-2%
      Set _minute=%_minute:~-2%
      


set /a _dd+=1
Set z1=%_mm%/%_dd%/%_yyyy%
set /a _dd+=1
Set z2=%_mm%/%_dd%/%_yyyy%
set /a _dd+=1
Set z3=%_mm%/%_dd%/%_yyyy%
set /a _dd+=1
Set z4=%_mm%/%_dd%/%_yyyy%
set /a _dd+=1
Set z5=%_mm%/%_dd%/%_yyyy%
set /a _dd+=1
Set z6=%_mm%/%_dd%/%_yyyy%
set /a _dd+=1
Set z7=%_mm%/%_dd%/%_yyyy%
set /a _dd+=1
Set z8=%_mm%/%_dd%/%_yyyy%
set /a _dd+=1
Set z9=%_mm%/%_dd%/%_yyyy%
set /a _dd+=1
Set z10=%_mm%/%_dd%/%_yyyy%
set /a _dd+=1
Set z11=%_mm%/%_dd%/%_yyyy%
set /a _dd+=1
Set z12=%_mm%/%_dd%/%_yyyy%
set /a _dd+=1
Set z13=%_mm%/%_dd%/%_yyyy%
set /a _dd+=1
Set z14=%_mm%/%_dd%/%_yyyy%
set /a _dd+=1
Set z15=%_mm%/%_dd%/%_yyyy%


Echo %z1%,12:15 AM,5:00 AM,10:00 AM,12:15 PM,5:00 PM,10:00 PM, >> dates.csv
Echo %z2%,12:15 AM,5:00 AM,10:00 AM,12:15 PM,5:00 PM,10:00 PM, >> dates.csv 
Echo %z3%,12:15 AM,5:00 AM,10:00 AM,12:15 PM,5:00 PM,10:00 PM, >> dates.csv 
Echo %z4%,12:15 AM,5:00 AM,10:00 AM,12:15 PM,5:00 PM,10:00 PM, >> dates.csv 
Echo %z5%,12:15 AM,5:00 AM,10:00 AM,12:15 PM,5:00 PM,10:00 PM, >> dates.csv 
Echo %z6%,12:15 AM,5:00 AM,10:00 AM,12:15 PM,5:00 PM,10:00 PM, >> dates.csv 
Echo %z7%,12:15 AM,5:00 AM,10:00 AM,12:15 PM,5:00 PM,10:00 PM, >> dates.csv 
Echo %z8%,12:15 AM,5:00 AM,10:00 AM,12:15 PM,5:00 PM,10:00 PM, >> dates.csv 
Echo %z9%,12:15 AM,5:00 AM,10:00 AM,12:15 PM,5:00 PM,10:00 PM, >> dates.csv 
Echo %z10%,12:15 AM,5:00 AM,10:00 AM,12:15 PM,5:00 PM,10:00 PM, >> dates.csv 
Echo %z11%,12:15 AM,5:00 AM,10:00 AM,12:15 PM,5:00 PM,10:00 PM, >> dates.csv 
Echo %z12%,12:15 AM,5:00 AM,10:00 AM,12:15 PM,5:00 PM,10:00 PM, >> dates.csv 
Echo %z13%,12:15 AM,5:00 AM,10:00 AM,12:15 PM,5:00 PM,10:00 PM, >> dates.csv 
Echo %z14%,12:15 AM,5:00 AM,10:00 AM,12:15 PM,5:00 PM,10:00 PM, >> dates.csv 
Echo %z15%,12:15 AM,5:00 AM,10:00 AM,12:15 PM,5:00 PM,10:00 PM, >> dates.csv 
  
 
pause

The exported .csv need to be like this.

10/28/2022  12:15 AM    5:00 AM  10:00 AM   12:15 PM    5:00 PM  10:00 PM
10/29/2022  12:15 AM    5:00 AM  10:00 AM   12:15 PM    5:00 PM  10:00 PM
10/30/2022  12:15 AM    5:00 AM  10:00 AM   12:15 PM    5:00 PM  10:00 PM
10/31/2022  12:15 AM    5:00 AM  10:00 AM   12:15 PM    5:00 PM  10:00 PM
11/1/2022   12:15 AM    5:00 AM  10:00 AM   12:15 PM    5:00 PM  10:00 PM
11/2/2022   12:15 AM    5:00 AM  10:00 AM   12:15 PM    5:00 PM  10:00 PM
11/3/2022   12:15 AM    5:00 AM  10:00 AM   12:15 PM    5:00 PM  10:00 PM
11/4/2022   12:15 AM    5:00 AM  10:00 AM   12:15 PM    5:00 PM  10:00 PM

Thank you for your help


Solution

  • There can be used the following batch file for this task:

    @echo off
    setlocal EnableExtensions DisableDelayedExpansion
    if "%~1" == "" (
        rem Get local date and time in a region independent format.
        for /F "skip=1 tokens=1 delims=." %%D in ('%SystemRoot%\System32\wbem\wmic.exe OS get LocalDateTime') do set "LocalDateTime=%%D" & goto GetDate
    ) else (
        rem This is for fast testing determining the date of tomorrow from any
        rem date specified as parameter in format yyyyMMdd on calling this batch
        rem file from within a command prompt window. The parameter string is
        rem not validated at all as this is just for testing the code below.
        set "LocalDateTime=%~1"
    )
    
    rem Get day, month and year from the local date/time string (or parameter).
    :GetDate
    set "Day=%LocalDateTime:~6,2%"
    set "Month=%LocalDateTime:~4,2%"
    set "Year=%LocalDateTime:~0,4%"
    
    (for /L %%I in (1,1,60) do call :CalculateDate)>dates.csv
    goto EndBatch
    
    :CalculateDate
    rem Increase the day in month by 1 in any case.
    
    rem It is necessary to remove leading 0 for the days 08 and 09 as
    rem those two days would be otherwise interpreted as invalid octal
    rem numbers and increment result would be 1 instead of 9 and 10.
    rem if "%Day:~0,1%" == "0" set "Day=%Day:~1%"
    rem set /A Day+=1
    
    rem Faster is concatenating character 1 with the day string to string
    rem representing 101 to 131 and subtract 99 to increment day by one.
    set /A Day=1%Day%-99
    
    rem The tomorrow's date is already valid if the day of month is less than 29.
    if %Day% LSS 29 goto BuildTomorrow
    
    rem Tomorrow is next month if day is equal (or greater) 32.
    if %Day% GEQ 32 goto NextMonth
    
    rem Day 31 in month is not possible in April, June, September and November.
    rem In February it can't occur that day in month increased from 30 to 31
    rem except on calling this batch file with invalid date string 20160230.
    if %Day% EQU 31 (
        if %Month% == 04 goto NextMonth
        if %Month% == 06 goto NextMonth
        if %Month% == 09 goto NextMonth
        if %Month% == 11 goto NextMonth
    )
    
    rem The day 29 and 30 in month is valid for all months except February.
    if NOT %Month% == 02 goto BuildTomorrow
    
    rem Determine if this year is a leap year with 29 days in February.
    set /A LeapYearRule1=Year %% 400
    set /A LeapYearRule2=Year %% 100
    set /A LeapYearRule3=Year %% 4
    
    rem The current year is always a leap year if it can be divided by 400
    rem with 0 left over (1600, 2000, 2400, ...). Otherwise if the current
    rem year can be divided by 100 with 0 left over, the current year is NOT
    rem a leap year (1900, 2100, 2200, 2300, 2500, ...). Otherwise the current
    rem year is a leap year if the year can be divided by 4 with 0 left over.
    rem Well, for the year range 1901 to 2099 just leap year rule 3 would be
    rem enough and just last IF condition would be enough for this year range.
    
    set "LastFebruaryDay=28"
    if %LeapYearRule1% == 0 (
        set "LastFebruaryDay=29"
    ) else if NOT %LeapYearRule2% == 0 (
        if %LeapYearRule3% == 0 (
             set "LastFebruaryDay=29"
        )
    )
    if %Day% LEQ %LastFebruaryDay% goto BuildTomorrow
    
    rem Tomorrow is next month. Therefore set day in month to 1, increase the
    rem month by 1 and if now greater than 12, set month to 1 and increase year.
    :NextMonth
    set "Day=1"
    set /A Month=1%Month%-99
    if %Month% GTR 12 (
        set "Month=1"
        set /A Year+=1
    )
    
    rem The leading 0 on month and day in month could be removed and so both
    rem values are defined again as string with a leading 0 added and next just
    rem last two characters are kept to get day and month always with two digits.
    :BuildTomorrow
    set "Day=0%Day%"
    set "Day=%Day:~-2%"
    set "Month=0%Month%"
    set "Month=%Month:~-2%"
    
    rem Output the line with next date written into the CSV file.
    echo %Month%/%Day%/%Year%,12:15 AM,5:00 AM,10:00 AM,12:15 PM,5:00 PM,10:00 PM
    goto :EOF
    
    :EndBatch
    endlocal
    

    Please read the remarks - the lines beginning with command rem - for an explanation. These lines can be all removed for faster execution of the batch file.

    The code is from Windows batch event reminder for next day. I have just added a loop with for /L for 60 days and put the code to calculate the next day and the output of the line to write into the CSV file into a subroutine named CalculateDate.