Reputation: 15
I need to split one file when ever the empty records starts,File contains multiple file structure in the same file need to split the files using batch script.
Important, that every sub file is separated by empty record
so the data look like: test.csv
sno,employee name,address,location,zip code
1,aaaa,12/34,Hyderabad,500089
2,bbbb,12/35/44,chennai,500079
3,cccc,12/31/11,pune,500069
Cardnumber,cardname,card type,card limit
12345,visa,diamond,10000
2345,master,platinum,50000
Accno,bank name,branch code,Branch location
98765,sbi,23456,hyd
12457,citi,8765,usa
4444,axis,78767,India
and I need separate file like below Test1.csv
sno,employye name,address,location,zipcode
1,aaaa,12/34,Hyderabad,500089
2,bbbb,12/35/44,chennai,500079
3,cccc,12/31/11,pune,500069
Test2.csv
Cardnumber,cardname,card type,card limit
12345,visa,diamond,10000
2345,master,platinum,50000
Test3.csv
Accno,bank name,branch code,Branch location
98765,sbi,23456,hyd
12457,citi,8765,usa
4444,axis,78767,india
I have tried below script but it is not working as expected.
@echo off & setlocal enabledelayedexpansion
set c=0
for /f "tokens=*" %%a in (test.csv) do (
if "%%a" equ "\n" (
set /a c+=1
>f!c!.csv echo.
) else (
>> f!c!.csv echo %%a
)
)
Please help me to write the batch script for the above requirement. Thanks in Advance
Upvotes: 1
Views: 392
Reputation: 56180
for /f
ignores empty lines. So you need a little trick for lines not to be empty. find /n
adds a line number to each line, so it isn't empty any more.
@echo off
setlocal enabledelayedexpansion
set c=0
for /f "tokens=1,* delims=]" %%a in ('type t.txt^|find /n /v ""') do (
REM echo A %%a B %%b
if "%%b" equ "" (
set /a c+=1
>f!c!.csv echo.
) else (
>> f!c!.csv echo %%b
)
)
The REM
line is left from troubleshooting, but I left it there because it may help you understand, what's going on.
Upvotes: 1