Though it may take hell lot of a time for me to understand this piece of code, this is a very useful Windows batch script split a text file based on number of lines. The condition is both the input file and output directory should exist. Number of lines is specified as MaxLines.

@echo off
setlocal EnableDelayedExpansion

set InFile=E:\Wordpress\final\Reading\b\in.txt
set OutDir=E:\Wordpress\final\Reading\b
REM Can not be larger than 2147483648 !!!
set MaxLines=
10000

if not exist “%InFile%” (
echo *ERROR* Input file does not exist!
exit /b
)

if not exist “%OutDir%\” (
echo *ERROR* Output folder does not exist!
exit /b
)

for %%A in (“%InFile%”) do (
set Name=%%~nA
set Ext=%%~xA
)

set /a Line=MaxLines+1
set File=0
for /f “usebackq tokens=*” %%A in (“%InFile%”) do (
set /a Line+=1
if !Line! GTR %MaxLines% (
set /a File+=1
set OutFile=%OutDir%\%Name%_!File!%Ext%
if exist “!OutFile!” del “!OutFile!”
set Line=1
)
echo.%%A>>”!OutFile!”
)

Advertisements