Batch file to split .csv file

前端 未结 6 784
醉酒成梦
醉酒成梦 2020-11-29 04:44

I have a very large .csv file (>500mb) and I wish to break this up into into smaller .csv files in command prompt. (Basically trying to find a linux \"split\" function in Wi

6条回答
  •  失恋的感觉
    2020-11-29 05:15

    I found this question while looking for a similar solution. I modified the answer that @Dale gave to suit my purposes. I wanted something that was a little more flexible and had some error trapping. Just thought I might put it here for anyone looking for the same thing.

    @echo off
    setLocal EnableDelayedExpansion
    GOTO checkvars
    
    :checkvars
        IF "%1"=="" GOTO syntaxerror
        IF NOT "%1"=="-f"  GOTO syntaxerror
        IF %2=="" GOTO syntaxerror
        IF NOT EXIST %2 GOTO nofile
        IF "%3"=="" GOTO syntaxerror
        IF NOT "%3"=="-n" GOTO syntaxerror
        IF "%4"==""  GOTO syntaxerror
        set param=%4
        echo %param%| findstr /xr "[1-9][0-9]* 0" >nul && (
            goto proceed
        ) || (
            echo %param% is NOT a valid number
            goto syntaxerror
        )
    
    :proceed
        set limit=%4
        set file=%2
        set lineCounter=1+%limit%
        set filenameCounter=0
    
        set name=
        set extension=
    
        for %%a in (%file%) do (
            set "name=%%~na"
            set "extension=%%~xa"
        )
    
        for /f "usebackq tokens=*" %%a in (%file%) do (
            if !lineCounter! gtr !limit! (
                set splitFile=!name!_part!filenameCounter!!extension!
                set /a filenameCounter=!filenameCounter! + 1
                set lineCounter=1
                echo Created !splitFile!.
            )
            cls
            echo Adding Line !splitFile! - !lineCounter!
            echo %%a>> !splitFile!
            set /a lineCounter=!lineCounter! + 1
        )
        echo Done!
        goto end
    :syntaxerror
        Echo Syntax: %0 -f Filename -n "Number Of Rows Per File"
        goto end
    :nofile
        echo %2 does not exist
        goto end
    :end
    

提交回复
热议问题