find text lines and adding empty line

June 20, 2010 at 14:33:56
Specs: Windows 7, 2.83
Found this batch and and used it for subtitles.

@echo off
setlocal enabledelayedexpansion
set strg1=sub fr.srt
set strg2=sub eng.srt
set file=sub-fr.srt
set newfile=new sub fr.srt
del "%newfile%"
set pos1=24
set pos2=49
::numbers 24 and 49 are arbitrary
::count positions starting from 0
set /a range=%pos2%-%pos1%
for /f "usebackq delims=" %%a in ("!file!") do (
set line=%%a
set strline=!line:~%pos1%,%range%!
echo !strline!>"%temp%\#"
call :findx
)
goto :eof

:findx
findstr "%strg1%" "%temp%\#">nul
set change=no
if %errorlevel%==0 call :change
if %change%==no echo !line!>>"!newfile!
goto :eof

:change
set change=yes
set line=!line:%strline%=BaAtTcChHh!
set strline=!strline:%strg1%=%strg2%!
set xline=!line:BaAtTcChHh=%strline%!
echo !xline!>>"%newfile%"
goto :eof

This batch reads the original time lines from "sub eng.srt" and placed them in "sub-fr.srt" So the "sub-fr.srt" is in sync. (btw it alway giving an error cannot find(first run) )

I've got two questions regarding the batch above.

1) set pos1=24
set pos2=49
is used for ?? (saw some speed gain)

2)
subtitles (srt) contains at least three lines
a) line number
b) time line
c) text line

The problem is that c (text line) could be more than one (two or even three). The batch should find out how much and add an empty line. So the output would be
a
b
c
empty line
a
b
c
And not like it does now
a
b
c
a
b
c
etc.


See More: find text lines and adding empty line

Report •


#1
June 20, 2010 at 14:46:46
set pos1=24
set pos2=49
....
set /a range=%pos2%-%pos1%
....
set strline=!line:~%pos1%,%range%!


They are being used for substring ranges i.e skip the first 24 characters and output the next 25 characters.


Report •

#2
June 20, 2010 at 14:57:41
As for the empty line issue this may help:

<untested>

for /f "delims=" %%a in (' find /n /v "" ^< "!file!" ') do (
    set line=%%a
    set line=!line:*]=!
    if defined line (
        set strline=!line:~%pos1%,%range%!
        echo !strline!>"%temp%\#"
        call :findx
    ) else (
        >> "!newfile!" echo.
    )
)


Report •

#3
June 20, 2010 at 15:29:58
Damn you are fast and good

Problem resolved (emty line).


Report •

Related Solutions

#4
June 20, 2010 at 15:59:43
Your kind ;)

I'm guessing the error you mention(I missed that part on the first pass) is coming from:

del "%newfile%"

It should do no harm, %newfile% doesn't exist and therefore can't deleted. You can replace it with:

type nul > "%newfile%"

Or even:

@echo off > "%newfile%"

And it will have the same effect without the error.


Also:

if %change%==no echo !line!>>"!newfile!

Is missing a closing double quote.


Report •

#5
June 21, 2010 at 10:07:15
Thanks again!

i'm not a 'program' expert give me a soldering iron and i'll fix anything.

added type nul > "%newfile%" (is actual looking for)
'echo off' only supress the error message.

double quote (even not in the original batch, it's added now)

Again thanks.
And now this batch saves a lot of time synchronsing your own language file using the original one (time tables) putting it in a third subtitle file without touching the translation text.
No stupid use for sync programs and ours of work.
(They are being used for substring ranges i.e skip the first 24 characters and output the next 25 characters.) Increase this A LOT is speeding it up to around 50% (7 min into 3,5 min around 1400 lines). Does not affect text in any way.


Report •

#6
June 21, 2010 at 17:55:00
Glad you got it working ;)


'echo off' only supress the error message.

Actually it makes output less verbose(not affecting error messages), but you can also use it to truncate a file to zero bytes if you "> file".

To suppress error messages you have to redirect them to nul:

2>nul command


Report •

Ask Question