Home > Mobile >  Batch file: Export data in a CSV file
Batch file: Export data in a CSV file

Time:10-08

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

CodePudding user response:

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 "           
  • Related