批量拆分多个CSV文件

时间:2016-11-17 21:44:55

标签: windows batch-file windows-console

我在SO上使用了这个脚本:

Batch file to split .csv file

@echo off
setLocal EnableDelayedExpansion

set limit=50
set file=export.csv
set lineCounter=1
set filenameCounter=1

set 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
)

这会将一个文件拆分成几个较小的文件(每行50行)。我希望将它拆分为特定/当前目录中的所有文件。

1 个答案:

答案 0 :(得分:0)

最终代码归功于@DavidPostill

@echo off
setLocal EnableDelayedExpansion
for /f "tokens=*" %%f in ('dir /b *.csv') do (

    set limit=100 
    set lineCounter=1
    set filenameCounter=1

    set name=
    set extension=
    for %%a in (%%f) do (
        set "name=%%~na"
        set "extension=%%~xa"
    )

    for /f "tokens=*" %%a in (%%f) 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
    )
)