Batch file to split .csv file Batch file to split .csv file windows windows

Batch file to split .csv file


Try this out:

@echo offsetLocal EnableDelayedExpansionset limit=20000set file=export.csvset lineCounter=1set filenameCounter=1set name=set extension=for %%a in (%file%) do (    set "name=%%~na"    set "extension=%%~xa")for /f "tokens=*" %%a in (%file%) do (    set splitFile=!name!-part!filenameCounter!!extension!    if !lineCounter! gtr !limit! (        set /a filenameCounter=!filenameCounter! + 1        set lineCounter=1        echo Created !splitFile!.    )    echo %%a>> !splitFile!    set /a lineCounter=!lineCounter! + 1)

As shown in the code above, it will split the original csv file into multiple csv file with a limit of 20 000 lines. All you have to do is to change the !file! and !limit! variable accordingly. Hope it helps.


Use the cgwin command SPLIT.Samples

To split a file every 500 lines counts:

split -l 500 [filename.ext]

by default, it adds xa,xb,xc... to filename after extension

To generate files with numbers and ending in correct extension, use following

split -l 1000 sourcefilename.ext destinationfilename -d --additional-suffix=.ext

the position of -d or -l does not matter,

  • "-d" is same as −−numeric−suffixes
  • "-l" is same as --lines

For more: split --help