Solved Batch script to split a text file into multiple files

June 7, 2013 at 13:00:59
Specs: Windows 7
Please find below my code to split the file :

@Echo off


Set /a xxFileNo=1
Set /a xxRecLimit=25
Set /a xxRecCount=0
Set /a cnt=0

Set "xxOutFile=D:\MyTextFileName_!xxFileNo!.txt"
Set "xxInFile=D:\MyTextFileName.txt"

for /f "tokens=* delims=^|" %%1 in (!xxInFile!) Do (

Echo %%1>>!xxOutFile!
Set /a xxRecCount+=1
set /a cnt+=1

if !cnt! gtr !xxRecLimit! (
Set /a xxFileNo+=1
Set "xxOutFile=D:\MyTextFileName_!xxFileNo!.txt"

Set "Header="MyCustom Header Line"
Echo !Header!>>!xxOutFile!
Set /a xxRecCount-=1
Set /a cnt=1

This code misses few records while writing to the output file. For example i have 100 records, each file should have 25 records in them. But this code only writes 17 record lines to the fourth file.

Can anyone help me in this code. It is little urgent and your help is much appreciated.

See More: Batch script to split a text file into multiple files

Report •

June 7, 2013 at 18:16:18
Here's what I would do for this:
@echo off & setlocal enabledelayedexpansion
Set "xxInFile=D:\MyTextFileName.txt"
Set xxOutFile=D:\MyTextFileName_
set c=0
for /f "tokens=*" %%a in (xxInFile) do (
set /a z=c/25+1
>>%xxoutfile%!z!.txt echo %%a
set /a c+=1
::===== end
might need some tweaking, but it worked on my end.

Report •

June 10, 2013 at 05:11:04
✔ Best Answer
thanks nbrane...

now i got the actual reason for missing records. They are missing because of their length. For these lines i am getting input line is too long error. Can you guide me in reading these lines also.

thanks in advance

Report •

June 10, 2013 at 12:31:11
Ah. In that case, batch is probably not do-able. (maybe there's a way, but I don't know it). Vbscript might serve:
'=== begin vbscript
set fso=createobject("scripting.filesystemobject")
set x=fso.opentextfile("d:\mytextfile.txt",1)
for j=1 to 3
set out=fso.opentextfile("file"+ltrim(cstr(j)),2,true)
for i=1 to 25
out.writeline x.readline
'===== end vbscript

Report •
Related Solutions

Ask Question