Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello guys, i want to do a batch file to split a text file into multiple txt files every time there is a given word inside the txt.for example:
(content of text.txt)
bla bla bla bla
CAR No. 1
bla bla bla bla
bla bla bla bla
CAR No. 2
bla bla bla bla
bla bla bla bla
bla bla bla bla
CAR No. 3and i want to split it into 3 txt files, the first of them containing CAR No.1 and bla bla bla...
the second containing CAR No.2 and bla blaand so on.
thanks in advance.

@echo off & setLocal EnableDELAYedExpansion
if exist file?.txt del file?.txt
for /f "tokens=* delims= " %%a in (text.txt) do (
set str=%%a
echo !str! | find "CAR No" > nul
if not errorlevel 1 (
set dest=!str!
if defined dest set dest=!dest:~-1!
)
if defined dest echo !str!>> file!dest!.txt
)
=====================================
Helping others achieve escape felicityM2

XmlMax will do this using an xpath expression. It will do it in a single pass using the unqiue file names option, but it does not run in batch mode. It is a GUI program. Get the free trial from xponentsoftware

It obviously wasn't urgent.
=====================================
Helping others achieve escape felicityM2

# Script SplitFile.txt var str file, str var str content ; cat $file > $content while ( { sen -c -r "^"+str+"&\n^" $content } > 0 ) do echo "\n\n\n\nNEXT SPLIT FOLLOWS.\n" stex -c -r "^"+str+"&\n^]" $content done echo "\n\n\n\nLAST SPLIT FOLLOWS.\n" echo $contentScript is in biterscripting ( http://www.biterscripting.com ) . Save the script in file C:/Scripts/SplitFile.txt, call it using the command
script "C:/Scripts/SplitFile.txt" file(C:/text.txt") str("CAR No.")Will show you splits of file C:/text.txt split at the string "Car No.". To create split files, just redirect the output of the stex command with > . Increment the file name after each split.

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |