按行数批量拆分文本文件

Batch split text file by number of lines

我找遍了,找不到一个简单的 windows 批处理脚本来按行数拆分文本文件。

我有一个包含大约 150,000 行的 .txt 文件

我正在尝试创建一个批处理,每 1000 行拆分一次文本文件。

我也尝试过使用 Gsplit 但没有成功

如果你能找到类似的问题,请link我

非常感谢。

我认为找到这个问题的答案会对其他用户和 google

上的人有所帮助

您只需要一个 for 循环和一个计数器。

@echo off
setlocal enabledelayedexpansion

:: Ensure that the user passed the file to the script
if "%~1"=="" (
    echo Please provide a file to process. You can drag the file onto the script.
    exit /b
)

set "split_at=1000"
set "input_file_name=%~1"
set "output_file_base_name=%~n1"
set "split_count=1"
set "line_count=1"

:: Move to the directory where the file is
pushd %~dp1

for /f "delims=" %%A in (%input_file_name%) do (
    >>%output_file_base_name%.!split_count! echo(%%A
    set /a line_count+=1

    REM If we've reached the split_number, roll the log over
    if !line_count! gtr %split_at% (
        set line_count=1
        set /a split_count+=1
    )
)

popd