Solved split one file to multiple files based based of first word

September 19, 2019 at 12:20:22
Specs: Windows 10
ex i have a file.txt which contain data like
123;hello;test
123;print;test
234;hello;test;;
234;teest;he;llo
i need to a create a files like 123.txt and 234.txt etc based on first word in a file please any suggestion where
123.txt
123;hello;test
123;print;test
234.txt
234;hello;test;;
234;teest;he;llo

using .bat script

message edited by prahallad


See More: split one file to multiple files based based of first word

Reply ↓  Report •

#1
September 19, 2019 at 20:03:06
This seemed to work in my limited tests:
::===== begin batchscript test.bat, filename for testing 'test'
@echo off & setlocal
:: optional, remmed out, to clear "old" files
rem for /f "tokens=1 delims=;" %%a in ('findstr "^.*;"^< test') do del %%a.txt

for /f "tokens=1 delims=;" %%a in ('findstr "^.*;"^< test') do call :xx %%a
goto :eof

:xx
rem if existing, then already built
if exist %1.txt goto :eof
for /f "tokens=1 delims=;" %%b in ("%1") do findstr "^%1;*"<test>%1.txt
::---------- end of batch script 'test'

message edited by nbrane


Reply ↓  Report •

#2
September 20, 2019 at 02:36:04
Thanks nbrane the code working fine but I have a file size of 100 mb its taking too much time is there any other way to finish it fast which will helped alot

Reply ↓  Report •

#3
September 20, 2019 at 21:21:54
✔ Best Answer
::===== begin batchscript test.bat, filename for testing 'test'
@echo off & setlocal
:: optional, remmed out, to clear "old" files
rem for /f "tokens=1 delims=;" %%a in ('findstr "^.*;"^< test') do del %%a.txt
for /f "tokens=1 delims=;" %%a in ('findstr "^.*;"^< test') do call :xx %%a
goto :eof

:xx
rem if existing, then already built
if exist %1.txt goto :eof
findstr "^%1;*"<test>%1.txt
::---------- end of batch script 'test'
eliminated a redundant filter and for-loop (don't know why I did that in prev. post!)
maybe that saves some time, otherwise I probly can't help much further. good luck :)


Reply ↓  Report •
Related Solutions


Ask Question