I have this batch file to split a txt file:
@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
It works but it splits it line by line. How do i make it split it every 5000 lines. Thanks in advance.
Edit:
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
But i get an error saying: Not enough storage is available to process this command
This will give you the a basic skeleton. Adapt as needed
EDITED
As the comments indicated, a 4.3GB file is hard to manage.
for /f
needs to load the full file into memory, and the buffer needed is twice this size as the file is converted to unicode in memory.This is a fully ad hoc solution. I've not tested it over a file that high, but at least in theory it should work (unless 5000 lines needs a lot of memory, it depends of the line length)
AND, with such a file it will be SLOW
EDITED, again: Previous code will not correctly work for lines starting with a colon, as it has been used as a delimiter in the
for
command to separate line numbers from data.For an alternative, still pure batch but still SLOW
Test this: the input file is
"file.txt"
and output files are"splitfile-5000.txt"
for example.This uses a helper batch file called
findrepl.bat
- download from: https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.batPlace
findrepl.bat
in the same folder as the batch file or on the path.A limitation is the number of lines in the file and the real largest number is
2^31 - 1
where batch math tops out.If the input file has not empty lines, previous method may be modified in order to run faster.