批量分割一个文本文件

我有这个batch file来分割一个txt文件

@echo off for /f "tokens=1*delims=:" %%a in ('findstr /n "^" "PASSWORD.txt"') do for /f "delims=~" %%c in ("%%~b") do >"text%%a.txt" echo(%%c pause 

它的工作原理,但它逐行分割。 我如何让它每5000行分割一次。 提前致谢。

编辑:

 I have just tried this: @echo off setlocal ENABLEDELAYEDEXPANSION REM Edit this value to change the name of the file that needs splitting. Include the extension. SET BFN=passwordAll.txt REM Edit this value to change the number of lines per file. SET LPF=50000 REM Edit this value to change the name of each short file. It will be followed by a number indicating where it is in the list. SET SFN=SplitFile REM Do not change beyond this line. SET SFX=%BFN:~-3% SET /A LineNum=0 SET /A FileNum=1 For /F "delims==" %%l in (%BFN%) Do ( SET /A LineNum+=1 echo %%l >> %SFN%!FileNum!.%SFX% if !LineNum! EQU !LPF! ( SET /A LineNum=0 SET /A FileNum+=1 ) ) endlocal Pause exit 

但我得到一个错误说: Not enough storage is available to process this command

这会给你一个基本的骨架。 根据需要调整

 @echo off setlocal enableextensions disabledelayedexpansion set "nLines=5000" set "line=0" for /f "usebackq delims=" %%a in ("passwords.txt") do ( set /a "file=line/%nLines%", "line+=1" setlocal enabledelayedexpansion for %%b in (!file!) do ( endlocal >>"passwords_%%b.txt" echo(%%a ) ) endlocal 

EDITED

正如评论所示,一个4.3GB的文件很难管理。 for /f需要将完整的文件加载到内存中,并且所需的缓冲区是该大小的两倍,因为该文件在内存中被转换为unicode。

这是一个完全临时的解决方案。 我没有测试过它的文件,但至少在理论上它应该工作(除非5000行需要大量的内存,这取决于行长度)

而且,用这样的文件,它会很慢

 @echo off setlocal enableextensions disabledelayedexpansion set "line=0" set "tempFile=%temp%\passwords.tmp" findstr /n "^" passwords.txt > "%tempFile%" for /f %%a in ('type passwords.txt ^| find /c /v "" ') do set /a "nFiles=%%a/5000" for /l %%a in (0 1 %nFiles%) do ( set /a "e1=%%a*5", "e2=e1+1", "e3=e2+1", "e4=e3+1", "e5=e4+1" setlocal enabledelayedexpansion if %%a equ 0 ( set "e=/c:"[1-9]:" /c:"[1-9][0-9]:" /c:"[1-9][0-9][0-9]:" /c:"!e2![0-9][0-9][0-9]:" /c:"!e3![0-9][0-9][0-9]:" /c:"!e4![0-9][0-9][0-9]:" /c:"!e5![0-9][0-9][0-9]:" " ) else ( set "e=/c:"!e1![0-9][0-9][0-9]:" /c:"!e2![0-9][0-9][0-9]:" /c:"!e3![0-9][0-9][0-9]:" /c:"!e4![0-9][0-9][0-9]:" /c:"!e5![0-9][0-9][0-9]:" " ) for /f "delims=" %%e in ("!e!") do ( endlocal & (for /f "tokens=1,* delims=:" %%b in ('findstr /r /b %%e "%tempFile%"') do @echo(%%c)>passwords_%%a.txt ) ) del "%tempFile%" >nul 2>nul endlocal 

再次编辑 :以前的代码将不能正确的工作,以冒号开头的行,因为它已被用作for命令中的分隔符来分隔行号和数据。

对于另一个选择,仍然是纯批次,但仍然很慢

 @echo off setlocal enableextensions disabledelayedexpansion set "nLines=5000" set "line=0" for /f %%a in ('type passwords.txt^|find /c /v ""') do set "fileLines=%%a" < "passwords.txt" (for /l %%a in (1 1 %fileLines%) do ( set /p "data=" set /a "file=line/%nLines%", "line+=1" setlocal enabledelayedexpansion >>"passwords_!file!.txt" echo(!data! endlocal )) endlocal 

测试一下:输入文件是"file.txt" ,输出文件是"splitfile-5000.txt"

这使用一个名为findrepl.bat的帮助程序批处理文件 – 从以下网址下载: https : findrepl.bat

findrepl.bat放在与批处理文件相同的文件夹中或路径中。

 @echo off :: splits file.txt into 5000 line chunks. set chunks=5000 set /as=1-chunks :loop set /as=s+chunks set /ae=s+chunks-1 echo %s% to %e% call findrepl /o:%s%:%e% <"file.txt" >"splitfile-%e%.txt" for %%b in ("splitfile-%e%.txt") do (if %%~zb EQU 0 del "splitfile-%e%.txt" & goto :done) goto :loop :done pause 

一个限制是文件中的行数,实际最大的数字是2^31 - 1批量数学最高的地方。

 @echo off setlocal EnableDelayedExpansion findstr /N "^" PASSWORD.txt > temp.txt set part=0 call :splitFile < temp.txt del temp.txt goto :EOF :splitFile set /A part+=1 (for /L %%i in (1,1,5000) do ( set "line=" set /P line= if defined line echo(!line:*:=! )) > text%part%.txt if defined line goto splitFile exit /B 

如果输入文件没有空行,则可以修改以前的方法以便运行得更快。