Split a Large CSV file into Smaller batches using Batch Scripting

Split a Large CSV file into Smaller batches using Batch Scripting

There are plenty of online tools to combine multiple csv files into a single one. But I could not find any to create multiple smaller (controlled) batches from a large csv file. Manually doing it is hectics, then found a way to automatically create smaller batches using bat file

Got this idea from stackoverflow ..

But it does not serves my purpose completely, I needed a controlled smaller batch, so that I can use those in my other application .. Hence, updated the bat file , explicitly mentioning how many rows to put in each file etc.

@echo off
setlocal ENABLEDELAYEDEXPANSION

:: Configurations
set "inputFile=TODO.csv"       :: Name of your input CSV file
set "rowsPerFile=500"          :: Number of rows per output file

:: Variables
set /a rowCounter=0
set /a fileIndex=0
set "headerRow="
set "outputFile="

:: Read the header row first
for /F "tokens=*" %%H in (%inputFile%) do (
    set "headerRow=%%H"
    goto :processRows
)

:processRows
:: Read and process the input file, skipping the first (header) row
for /F "skip=1 tokens=*" %%A in (%inputFile%) do (
    if !rowCounter! EQU 0 (
        :: Start a new file
        set /a fileIndex+=1
        set "outputFile=TODO_part_!fileIndex!.csv"
        echo !headerRow! > !outputFile!  :: Write the header to the new file
    )

    :: Append the current row to the output file
    echo %%A >> !outputFile!

    :: Increment the row counter and check for file splitting
    set /a rowCounter+=1
    if !rowCounter! GEQ %rowsPerFile% (
        set /a rowCounter=0
    )
)

echo Splitting complete! %fileIndex% files created.
pause

Now, keeping both my Large csv file (TODO.csv) and bat file (split_csv.bat) in the same folder. and double clicking my bat file giving me all the smaller CSVs, which now i can use for other purposes… cool stuff.