0

i would like to create a batch file that goes through txt files in a directory and every line that has #EXTINF in it, remove the last 15 characters..

basically if it is possible, i would like to trim the last 15-20 characters from any lines that is longer than a certain number. for some reason i have a software that if the line is too long, it will screw up the data.

thanks

user2314297
  • 11
  • 1
  • 5
  • 2
    Do you realize that these are two _entirely different_ questions? Which one do you want to solve? If you said "remove the last 15-20 characters" we do NOT know what to do! – Aacini Jun 02 '13 at 13:26
  • Also, might the #EXTINF be part of the text that is to be removed? sometimes? always? never? – dbenham Jun 02 '13 at 16:17
  • hi @Aacini. thank you for replying. i need the script to go through all files in a directory, and each line that starts with #extinf, remove XX number of characters from the end. for some reason my script output bad data if the line's number of characters exceeded 600. – user2314297 Jun 02 '13 at 17:07
  • And why is it OK to simply remove the last XX characters? Are they not also important? Why are they there in the first place? – dbenham Jun 02 '13 at 17:28
  • So you mean you want to preserve only the first 600 characters whenever a line starts with `#extinf`? – dbenham Jun 02 '13 at 17:29
  • hi. it is safe to remove since it is only a description line that its soul purpose is to add a description. but if it is longer than 600 characters, the whole line is skipped. – user2314297 Jun 03 '13 at 06:33

2 Answers2

1

See How can you find and replace text in a file using the Windows command-line environment? for various options to modify text files using Windows batch.

The following simple code uses REPL.BAT to truncate long lines that begin with #extinf (case insensitive) at 600 characters. Lines that are less than 600 characters are preserved in their entirety.

type test.txt|repl.bat "^(#extinf.{593}).*$" "$1" I>test.txt.new
move /y test.txt.new test.txt >nul
Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • looks great. any chance you have an idea how to preserve non-english characters? the files have hebrew inside them and the new output files screws them up. i added CHCP 1255 but it didnt help – user2314297 Jun 03 '13 at 06:50
  • @user2314297 - That is unexpected. I performed a test on a file with all 255 ANSI byte codes and found no corruption. Is your file ANSI or Unicode? I haven't done any tests with Unicode - I wouldn't be shocked if changes have to be made in REPL.BAT to support Unicode. – dbenham Jun 03 '13 at 11:48
  • The TYPE command will convert the content to ANSI by default. Eliminating the TYPE and pipe and replacing with redirection will preserve the unicode input, but REPL.BAT is not written to support unicode. I don't know what changes are required to support unicode. – dbenham Jun 04 '13 at 11:46
  • if i remove the TYPE & Pipe and leaving the redirection, how can i tell the script to process only test.txt file? – user2314297 Jun 05 '13 at 07:55
  • once i converted the output file into unicode (UTF8) it worked great. so the question is how can i output the file in UTF 8 – user2314297 Jun 05 '13 at 09:46
0

User231429 wrote: "i need the script to go through all files in a directory, and each line that starts with #extinf, remove XX number of characters from the end."

The Batch file below do precisely that:

@echo off
setlocal EnableDelayedExpansion

for %%f in (*.txt) do (
   (for /F "usebackq delims=" %%a in ("%%f") do (
      set "line=%%a"
      if "!line:~0,7!" equ "#extinf" set "line=!line:~0,-XX!"
      echo !line!
   )) > "%%~Nf.new"
)

REM del *.txt
REM ren *.new *.txt

Note that you must replace XX by a number in the long if command.

Test this program and check the result in *.NEW files. If the result is correct, remove REM part from two last lines.

This program remove exclamation marks from the file. This detail may be fixed, if required.

Aacini
  • 65,180
  • 12
  • 72
  • 108
  • checking this. any idea how i can add a line check so only lines that has more than 600 characters will be trimmed? – user2314297 Jun 03 '13 at 06:35
  • use `set "line=!line:~0,600!"` instead of `set "line=!line:~0,-XX!"`. That will shorten the line to max 600 chars, but leaves it as is if it has less than 600 chars. – Stephan Jun 03 '13 at 17:39
  • seems to work. any idea how to support unicode? when i save the batch file in utf8 it screws up the output file. i need it to support hebrew – user2314297 Jun 06 '13 at 10:02