get text from file.

June 28, 2010 at 12:51:57
Specs: Windows XP
Hi,

I have a text file i need to split into multiple text files on certain special characters.

I want to make a batch file that will:
- open .txt file
- split the text file into variables on every ~* characters
- make a NEW text file for every variable that is made.

Any help/direction would be appreciated

This is my first time here so thanks =)


See More: get text from file.

Report •


#1
June 28, 2010 at 16:18:34
is "~*" on a line by itself, or can one or more be embedded in the midst of a line? examples:

a)
this
is
file 1
~*
example
one
file 2

b)
this ~*line will be~* in three different files!
~*new file 4~*new file 5

(big difference in coding!)
or am i completely missing the mark, and
"~*" means a set number, like 46 characters?


Report •

#2
June 30, 2010 at 05:56:57
It's just like option B! Thanks, i've never done this kind of coding before so i know nothing!

Report •

#3
June 30, 2010 at 12:44:45
[No credit where credit's not due department: the 'ProcessTokens' routine in this script is mostly lifted from
an old discussion in this forum; I've swapped it about a
bit and added a boolean variable 'HasTokens' to stop
it going into an endless loop if a line contains only
delimiters.]

Try the following (you'll need to change 'yourfile.txt' for
your input file name and It creates new files: 'newfile1.txt',
'newfile2.txt', etc.):

@echo off

setLocal EnableDelayedExpansion

set num=0
for /f "usebackq delims=" %%a in ("yourfile.txt") do (
  call :ProcessTokens "%%a" num
)
goto :eof


:ProcessTokens (str_byval, filenum_byref)
set line=%~1
:loop
set HasTokens=false
for /f "tokens=1* delims=*~" %%a in ("%line%") do (
  set /a %2+=1
  echo.%%a> newfile!%2!.txt
  set line=%%b
  if defined line set HasTokens=true
)
if "%HasTokens%" equ "true" goto loop


Report •

Related Solutions

#4
June 30, 2010 at 13:33:06
OMG you are a life saver! That is exactly perfect! thank you so much! I need to do one little tweek...

my original file looks like this:
~*I am the title for page 1, I am the narration for page 1. Page 1 is pretty neat.,~*I am the title for page 2, I am the narration for page 2. I am really cool.,~*I am the title for page 3, I am the narration for page 3.,~*I am the title for page 4, I am the narration for page 4.

So when the files get split they look like this:
newfile1:I am the title for page 1, I am the narration for page 1. Page 1 is pretty neat.,
newfile2:I am the title for page 2, I am the narration for page 2. I am really cool.,
etc, etc,

notice at the end of every file there is a comma. is there any way i can trim the comma off? so that each file end with a period? The last entry never has a comma, so maybe some type of an "if/else" statement?

P.S.- know any good sites i can learn this coding from? i've never seen it and i'm loving the possibilities!


Report •

#5
June 30, 2010 at 20:01:30
technically, there might be an issue with possibility of:
~*I am the title for page 1*, I am the narration~ for page 1. Page 1 is pretty neat.,~*I am the title for page 2~, I am the narration for* page 2. I am really cool.,~*I am the title for page 3, I am the narration* for page 3.,~*I am the title for page 4, I
am the narration for page 4.

since the delims= clause is single-character-based. (either ~ OR * will qualify as a delimiter, no requirement for ~ AND *).

another way, similar approach, hopefully fixing the comma thing too:
note: modified to fix the initial ~*
@echo off & setLocal EnableDelayedExpansion

set num=0
set yy=,
for /f "usebackq delims=" %%a in ("forum16") do (
set xx=!yy!%%a
:: that is ctrl-x in foll.line, but most ctrl chars could be subst.
set xx=!xx:,~*=!
set yy=
call :ProcessTokens "!xx!" num
)
goto :eof


:ProcessTokens
set line=%~1
:loop
for /f "tokens=1* delims=" %%a in ("%line%") do (
set /a %2+=1
echo.%%a> newfile!%2!.txt
set line=%%b
if defined line goto :loop
)


Report •

#6
July 1, 2010 at 02:19:52
nbrane wrote:

since the delims= clause is single-character-based. (either ~ OR * will qualify as a delimiter, no requirement for ~ AND *).

Oops, yeah - I overlooked that, er, not-so-minor detail whilst
concentrating on the unknown tokens aspect :)

It's still not quite right though - the CTRL-X trick doesn't
seem to be replacing the first occurence of '~*' since the
first output file leads with it. Not sure why though...

EDIT:

Scratch that, I didn't notice the comma in:
set xx=!xx:,~*=!
(Hmm... this is getting more and more kludgy
by the minute...) If you try to replace just '~*'
it's not interpreted correctly, probably assuming
that you're trying to do !xx:~0,... or something.
Looks that there is no non-trivial (or any) way
of tackling this particular problem in batch
reliably...


Report •

#7
July 1, 2010 at 04:43:13
lookatme0128 wrote:

P.S.- know any good sites i can learn this coding from? i've never seen it and i'm loving the possibilities!

Actually, I'm probably one of the last people you should ask that
question - I'm not very disciplined when it comes to learning stuff
and I tend to grab information from here, there and everywhere
depending on what aspect of something I'm interested at the
time or whether I have a need to know something...



Report •

#8
July 1, 2010 at 08:44:35
"P.S.- know any good sites i can learn this coding from? i've never seen it and i'm loving the possibilities!"

Try this one....

http://www.robvanderwoude.com/ntfor...


Report •

#9
July 2, 2010 at 06:45:09
OK, had to roll my sleeves up a little bit for this one (it's
not the easiest thing to do in batch...).

lookatme0128: (hopefully you're still around) Try the
following (rather that use for /f... to parse the tokens it
looks for your sequence '~*' char-by-char to parse
them and also removes the commas that you weren't
happy with):

@echo off

setLocal EnableDelayedExpansion

set num=0
for /f "usebackq delims=" %%a in ("yourfile.txt") do (
  call :ProcessTokens "%%a" num
)
goto :eof


:ProcessTokens (str_byval filenum_byref)
set line=%~1
set lineout=
set n1=0
:loop
set DelimsFound=false
set /a n2=%n1%+1
for %%i in (%n1%) do set Char=!line:~%%i,1!
for %%i in (%n2%) do set NextChar=!line:~%%i,1!
if "%char%" equ "~" if "%NextChar%" equ "*" set DelimsFound=true
if "%DelimsFound%" equ "true" (
  set /a n1+=1
  if defined lineout (
    if "!lineout:~-1!" equ "," set lineout=!lineout:~0,-1!
    set /a num+=1
    echo.!lineout!> newfile!%2!.txt
    set lineout=
  )
) else (
  if not defined lineout (
    set lineout=%char%
  ) else (
    set lineout=%lineout%%char%
  )
)
set /a n1+=1
if not defined char (
  if defined lineout (
    if "!lineout:~-1!" equ "," set lineout=!lineout:~0,-1!
    set /a num+=1
    echo.!lineout!> newfile!%2!.txt
  )
) else (
  goto loop
)



Report •

#10
July 6, 2010 at 07:29:32
Sorry, i went out of town for the holiday and just got back...

All of your solutions have worked great. However, my boss just changed the way he wanted it to work, so now i will have to make a whole new post to see what he wants is even possible.

Thank everyone for all your help and thank you ace_omega, i have learned a lot from that site already!


Report •

Ask Question