Solved split many text files

November 28, 2016 at 13:08:14
Specs: Windows 64
Hi,

I have a large number of text files (3000 files) and I want to split each file into two files using a phrase as the criteria. For example,

Section one
xxxxxxxxxxx
xxxxxxxxxxx
xxxxxxxxxxxx

Section two
xxxxxxxxx
xxxxxxxxx
xxxxxxxxxx

I want to have section one in one text file and section two in another text file.


Please advise me and help as I urgently need to do that.

Many thanks in advance for kind assistance.



See More: split many text files

Report •

#1
November 28, 2016 at 23:01:11
✔ Best Answer
::====== script starts here ===============
:: split txt files into two by Section one Section two tags
:: splittwo.bat 2016-11-29 13:50:45.59
@echo off & setLocal enableDELAYedeXpansioN

dir/b *.txt > myfile

:main
for /f "tokens=* delims= " %%a in (myfile) do (
call :sub1 %%a
)
goto :eof

:sub1
for /f "tokens=* delims= " %%i in (%1) do (
if "%%i" equ "Section one" set outfile=%%~Ni-one
if "%%i" equ "Section two" set outfile=%%~Ni-two
>> !outfile! echo.%%i
)
goto :eof
::====== script ends here =================

=====================

M2 Get custom script or take private lessons


Report •

#2
November 29, 2016 at 15:29:22
Hello Mechanix2go

Thank you for you kind reply and I appreciate that from you.

I am not very technical person so please excuse me for my questions.

First, what software or program I need to use to perform this task.

I am just little bit lost with these scripts. Can you please explain it a little bit more in less technical term.

Also, do I need to code each file individually? Or this will work once for all 3000 text files.

Many thanks once again.

message edited by yb9911


Report •

#3
November 29, 2016 at 23:35:52
It's a batch script, meaning you don't need any 3rd party software, and you only need to save it as a .bat file to make it execute. It'll work for all your files, not just one. You'd need to place the .bat file in the same directory as the files you want to process.

Don't worry if plan A fails, there are 25 more letters in the alphabet ;)


Report •

Related Solutions

#4
December 8, 2016 at 13:48:09
I got no idea how does it work. For the script is totally unclear and I do not know where to run it.

Report •

#5
December 9, 2016 at 01:03:12
Make a new directory, let's say:
c:\myfiles
Put your text files to splot on it.
Put the script in it.
Run the script JUST ONCE.
Note any messages on screen.
You should now have two files for each text file processed with names like:
filename-one
filename-two

=====================

M2 Get custom script or take private lessons


Report •

#6
December 9, 2016 at 02:37:11
unfortunately, it does not work.

I copy the script (modified section one and section two to what I need) to txt file and save it as .bat then put in the txt files folder and run it. It does not work.


Report •

#7
December 9, 2016 at 20:00:41
This error came up in my tests:
if "%%i" equ "Section one" set outfile=%%~Ni-one
if "%%i" equ "Section two" set outfile=%%~Ni-two

should be:
if "%%i" equ "Section one" set outfile=%~N1-one
if "%%i" equ "Section two" set outfile=%~N1-two

And another possible problem is if any trailing spaces present (after the end of the signal-string), the signal will not be recognized. This can be handled but requires some additional code. Wait for that shoe to drop...


Report •

#8
December 11, 2016 at 06:13:11
"unfortunately, it does not work"

It may help to say what it DOES.
Or doesn't does.

LOL someone would need to be WAY smarter than me to troubleshoot a script without seeing it.

=====================

M2 Get custom script or take private lessons


Report •

#9
December 11, 2016 at 06:36:36
Ok. Let say that my I want to split the each transcript based on the following phases:

================================================================================
Presentation
--------------------------------------------------------------------------------

xxxxxxx
xxxxxxx
xxxxxx
xxxxxxx

================================================================================
Questions and Answers
--------------------------------------------------------------------------------

xxxxxxxx
xxxxxxxxx
xxxxxxxxxx

and the folder directory is c:/Jimmy/desktop/transcripts

In this case how the script should be written for the .bat file.


Report •

#10
December 13, 2016 at 21:13:54
Make a list of your files into a text file like this:

> filelist.txt dir/b *.txt

Put the filelist.txt Nd you script in a ZIP and send it to the address in my site.

http://golden-triangle.com/Mechanix

=====================

M2 Get custom script or take private lessons


Report •

#11
December 20, 2016 at 23:34:03
I have looked at your files.

The string "question" appears here and there but not consistenly enough to use as a split indicator.

14:34 2016-12-21
http://golden-triangle.com/Mechanix

=====================

M2 Get custom script or take private lessons


Report •

Ask Question