Page 1 of 1

Add heading between lines in text document

Posted: 29 Jun 2013 22:07
by jeff p
I have a screenplay (text document) which I would like to indicate their separate lines of dialogue.
My text document is located here: C:\Projects\Scripts\Version01.txt
Is it possible for a batch script to accomplish this?


as an example: to go from this:

Bob_
Te vero tantas elaboraret usu, mollis latine dolorem qui ei. Cu consequat tincidunt eum, vel vocibus nusquam gloriatur ad.
Ea pri libris euismod, feugait mandamus et eam. Modo euismod constituam ad qui.
Quod consul dolorum his ad, hendrerit disputationi mea ei.
Alii duis accumsan.

Mary_
Et hinc elit disputationi vix, saperet copiosae eam no.
Tritani invenire omittantur est et, ea partiendo persequeris eam,

Tom_
Illum tibique constituto ea vix, autem eleifend te mei. Ancillae eligendi vis ne, ad duo invidunt delicatissimi,
Posse euismod dignissim in usu, ut maluisset euripidis pri. Nam simul noster comprehensam eu
Probo zril semper ius ei,


To this:

Bob_
Te vero tantas elaboraret usu, mollis latine dolorem qui ei. Cu consequat tincidunt eum, vel vocibus nusquam gloriatur ad.
Bob_
Ea pri libris euismod, feugait mandamus et eam. Modo euismod constituam ad qui.
Bob_
Quod consul dolorum his ad, hendrerit disputationi mea ei.
Bob_
Alii duis accumsan.

Mary_
Et hinc elit disputationi vix, saperet copiosae eam no.
Mary_
Tritani invenire omittantur est et, ea partiendo persequeris eam,

Tom_
Illum tibique constituto ea vix, autem eleifend te mei. Ancillae eligendi vis ne, ad duo invidunt delicatissimi,
Tom_
Posse euismod dignissim in usu, ut maluisset euripidis pri. Nam simul noster comprehensam eu
Tom_
Probo zril semper ius ei,


Thanks for any help!

Jeff

Re: Add heading between lines in trext document

Posted: 30 Jun 2013 00:14
by Aacini

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set name=
for /F "tokens=1,2 delims=:" %%a in ('findstr /N "^" C:\Projects\Scripts\Version01.txt') do (
   if not defined name (
      set "name=%%b"
   ) else (
      set "line=%%b"
      if defined line (
         echo !name!
         echo !line!
      ) else (
         set name=
         echo/
      )
   )
)


Antonio

Re: Add heading between lines in trext document

Posted: 30 Jun 2013 04:13
by foxidrive
Very clever Aacini.

I was going to write it in PureBasic as I thought it was too tricky for a simple batch.


The only issue may be ! characters if the screenplay has any.

Re: Add heading between lines in trext document

Posted: 30 Jun 2013 05:31
by penpen
There are two minor bugs in this batch program:
- if the text contains a colon, then all after that was cut of, and
- a missing endlocal

But this is easy to fix:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set name=
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" C:\Projects\Scripts\Version01.txt') do (
   if not defined name (
      set "name=%%b"
   ) else (
      set "line=%%b"
      if defined line (
         echo !name!
         echo !line!
      ) else (
         set name=
         echo/
      )
   )
)
endlocal

Re: Add heading between lines in trext document

Posted: 30 Jun 2013 06:57
by foxidrive
penpen wrote: - if the text contains a colon, then all after that was cut of, and
- a missing endlocal


Only a leading colon will be affected. That can be worked around too

and the endlocal is implied when a batch file terminates - it doesn't need one.

Re: Add heading between lines in trext document

Posted: 30 Jun 2013 07:34
by penpen
foxidrive wrote:Only a leading colon will be affected. That can be worked around too
No, Aacini has used the for option "tokens=1,2", that means only the first to tokens were passed to the for loop. It does not mean, that tokenizing ends after the first token and the rest of the line can be accessed as token two.
So if the for loop reaches such a line "Text1:Text2:Text3", this results in an output of "Text1"

To interrupt tokenizing you have to use "tokens=1*" and you get the full rest of the line within the second variable.

foxidrive wrote:and the endlocal is implied when a batch file terminates - it doesn't need one.
This behaviour can be changed somewhere in the windows registry, actually don't know the key to change anymore, but our admin has changed it (don't know why), so we always have to use endlocal.

Re: Add heading between lines in trext document

Posted: 30 Jun 2013 08:12
by Aacini
@penpen,

You are right about the first bug, but about the second one, this is an extract of SETLOCAL help:

Code: Select all

ENDLOCAL must be issued to restore the previous settings.  When the end
of a batch script is reached, an implied ENDLOCAL is executed for any
outstanding SETLOCAL commands issued by that batch script.

There is no way to change this behavior via a value in the registry.

Antonio

Re: Add heading between lines in trext document

Posted: 30 Jun 2013 15:59
by jeff p
Thanks for any help with this!

Looks as though It may be complicated.
My initial thought would be to indicate character names within the code: ie Bob_, Mary_, Tom_, etc.
to be used as the dividing area.


foxidrive wrote:Very clever Aacini.
The only issue may be ! characters if the screenplay has any.


Yes, I would be using full punctuation.

Thanks again for the help.

Jeff

Re: Add heading between lines in text document

Posted: 03 Jul 2013 19:54
by jeff p
The script provided doesn't work for me, despite my many attempts to modify.

... I assume it's too problematic to work out?

If so, I appreciate the effort!

Many thanks!

Jeff

Re: Add heading between lines in text document

Posted: 03 Jul 2013 20:02
by Aacini
@Jeff,

Excuse me, what exactly is the problem with my script? Please indicate what is the output obtained and what is the output you want. I can't fix a problem if I don't know what the problem is!

Antonio

Re: Add heading between lines in text document

Posted: 03 Jul 2013 22:13
by Squashman
This is the output I get running Antonio's code and the example input you provided.

Code: Select all

Bob_
Te vero tantas elaboraret usu, mollis latine dolorem qui ei. Cu consequat tincidunt eum, vel vocibus nusquam gloriatur ad.
Bob_
Ea pri libris euismod, feugait mandamus et eam. Modo euismod constituam ad qui.
Bob_
Quod consul dolorum his ad, hendrerit disputationi mea ei.
Bob_
Alii duis accumsan.

Mary_
Et hinc elit disputationi vix, saperet copiosae eam no.
Mary_
Tritani invenire omittantur est et, ea partiendo persequeris eam,

Tom_
Illum tibique constituto ea vix, autem eleifend te mei. Ancillae eligendi vis ne, ad duo invidunt delicatissimi,
Tom_
Posse euismod dignissim in usu, ut maluisset euripidis pri. Nam simul noster comprehensam eu
Tom_
Probo zril semper ius ei,

Re: Add heading between lines in text document

Posted: 03 Jul 2013 23:21
by jeff p
Hi Aacini

I've been trying to run the script. The dos prompt would appear and quickly disappear.
The results I anticipated were to have the modified text overwrite my original txt file, or output as a new one.
I wasn't sure If i needed to modify the code in any way.. ie insert the names as a variable.

After reading this post and seeing the results were obtained. I added a timeout in your code and saw that it was indeed making those exact changes inside the dos prompt itself.

Sorry for the confusion :oops:

.. Is it possible to have the results output as a new text file?

Thanks!

Jeff

Re: Add heading between lines in text document

Posted: 04 Jul 2013 04:08
by foxidrive
Change the last ) sign to:

Code: Select all

)>>"file.txt"

Re: Add heading between lines in text document

Posted: 04 Jul 2013 06:45
by Squashman
jeff p wrote:Is it possible to have the results output as a new text file?

I looked back at several topics you have started on this forum and several batch files that have been written for you already show you how to redirect output to a new file. Did you not understand any of the previous code you were given? Do you take the time to understand the code you are given?