用于拆分 .csv 文件的批处理文件 [英] Batch file to split .csv file

查看:79
本文介绍了用于拆分 .csv 文件的批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常大的 .csv 文件 (>500mb),我希望在命令提示符下将其分解为较小的 .csv 文件.(基本上是试图在Windows中找到一个linux拆分"功能.

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 Windows".

这必须是一个批处理脚本,因为我的机器只安装了 Windows,请求软件很麻烦.我遇到了一些示例代码(http://forums.techguy.org/software-development/1023949-split-100000-line-csv-into.html),但是,当我执行批处理时它不起作用.当我要求它每 20 000 行解析一次时,我得到的只是一个只有 125kb 的输出文件.

This has to be a batch script as my machine only has windows installed and requesting softwares is a pain. I came across a number of sample codes (http://forums.techguy.org/software-development/1023949-split-100000-line-csv-into.html), however, it does not work when I execute the batch. All I get is one output file that is only 125kb when I requested it to parse every 20 000 lines.

有没有人遇到过类似的问题,你们是如何解决这个问题的?

Has anyone ever come across a similar problem and how did you resolve the issue?

推荐答案

试试这个:

@echo off
setLocal EnableDelayedExpansion

set limit=20000
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
)

如上面的代码所示,它将原始csv文件拆分为多个csv文件,限制为20 000行.您所要做的就是相应地更改 !file!!limit! 变量.希望能帮助到你.

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.

这篇关于用于拆分 .csv 文件的批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆