0

I want to take a folder with several (15 in this first case) 'subtitle.srt' files (which, as I'm sure you're aware, are just text files with the extension ".srt" instead of ".txt") and modify each file in turn so that a new subtitle is added at the start of each file. I want the new subtitle to be:-

0
00:00:00,001 --> 00:00:02,100
"Filename"

So, for example, if the first subtitle file in the folder is called "01. What Lies Beneath.srt" and it looks like this:-

1
00:00:02,120 --> 00:00:03,560
<font color="#FFFF00">Previously on Superman & Lois...</font>

2
00:00:03,560 --> 00:00:06,880
<font color="#00FFFF">I'm stepping down from active duty.</font>
<font color="#00FF00">You're going to be hard to replace.</font>

3
Etc., etc...

then after processing, I want it to look like this:-

0
00:00:00,001 --> 00:00:02,100
01. What Lies Beneath

1
00:00:02,120 --> 00:00:03,560
<font color="#FFFF00">Previously on Superman & Lois...</font>

2
00:00:03,560 --> 00:00:06,880
<font color="#00FFFF">I'm stepping down from active duty.</font>
<font color="#00FF00">You're going to be hard to replace.</font>

3
Etc., etc...

I'm rubbish at batch coding so I tried searching out possible ways to do it but nothing I tried worked!

Below are some attempts I made using different "routines" I found; each successive attempt separated (from last to first) by the PAUSE, EXIT commands:-

for %%a in (*.txt) do type append_ns0 >> %%a.srt
pause
exit

for %%a in (*.txt) do type append_ns0 >> %%a
for %%a in (*.txt) do type "%%~na" >> %%a
for %%a in (*.txt) do type append_spc >> %%a.srt
pause
exit


for %%I in (*.txt) do copy "C:\Users\wbcam\Desktop\G classroom\AddTitle.txt"+"%%~nI"+" "+"%%I" "%%~nI.srt"
pause
exit

for %X in (C:\Users\wbcam\Desktop\G classroom\Add Titles\*.txt) do type C:\Users\wbcam\Desktop\G classroom\AddTitles.txt >> %X
pause
exit

To use the COPY command I had to first rename the files from .srt to .txt (I'd rather NOT have to do that; I'm hoping someone can show me how to work on the ,srt files without any intermediate stages) and COPY also seemed to add a hex1A character to the end of the new file but, of course, it couldn't handle the insertion of the Filename (a text string) into the new file as it would only concatenate files not strings (if I, eventually, understood it's operation correctly, Doh!). And attempts to use the ECHO or TYPE commands just seemed to overwrite everything in the original file leaving only:-

0
00:00:00,001 --> 00:00:02,100

and bugger all else!

Can anyone help out, please?

1 Answers1

0
@ECHO OFF
SETLOCAL
rem The following setting for the source directoryis a name
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"

FOR /f "delims=" %%b IN (
 'dir /b /a-d "%sourcedir%\*.srt" '
 ) DO (
 (
  ECHO 0
  ECHO 00:00:00,001 --^> 00:00:02,100
  ECHO "%%~nb"
  ECHO.
  TYPE "%sourcedir%\%%b"
 
 )>"%sourcedir%\%%~nb.txt"
 MOVE "%sourcedir%\%%~nb.txt" "%sourcedir%\%%b" >NUL
)

GOTO :EOF

Always verify against a test directory before applying to real data.

Perform a directory scan. assigning each filename that matches the mask to %%b.

Write the two required lines, a line containing the name part of the filename in quotes and an empty line to the output, then type the contents of the selected file. Note that the > character in the text needs to be escaped by a caret. The output of the echoes is gathered and redirected to a .txt file and the .txt file is then written over the original file. The 1 file(s) copied message is suppressed by the >nul.

If you prefer, you could replace the two echo lines that insert the fixed text with type somefilename where somefilename contains the fixed text required.

You could replace the move line for testing with

FC "%sourcedir%\%%~nb.txt" "%sourcedir%\%%b"

which will show the differences.

copy adds the control-z following an archaic convention that ^Z marked end-of-file for text files.

--- Addition in response to comment

for /? from the prompt generates documentation about the for command - in general, commandname /? generates documentation for any command, although some commands require -? or -h or --h or --help. Utility designers generally follow the same convention and the details depend on the platform for which the software was originally designed.

for /f ... ('command....') do ... interprets the command-output as though it was a file.

The dir command reports the filenames and subdirectorynames in a directory. By default, it produces a formatted report including filesize and last-update date, but has numerous options including /b to produces a "basic" report (names only, no sizes, headers, summary, dates ,etc.) and /a-d which suppresses directorynames. The /s option means 'and list subdirectories too'. It can also accept a filemask (or ambiguous filename) - which applies equally to directorynames. If the filename supplied contains a ? then this means any ONE character or * which means any number of any characters - any other characters are taken literally and will match regardless of case. Hence, *.srt means any number of any characters followed by .srtwhich should select all file/directorynames that end.srt. The filemask may be preceded by *directoryname\\* which means scan this directory- but *that* directoryname may **not** contain?or*`.

When for /f is supplied with a filename list in basic form, each filename in the list is assigned to the metavariable assigned (%%a in this example) in turn and the do statements are executed using the assigned value in the metavariable. Again, there are many ways of having for /f interpret the value-string that is assigned to the metavariable. Many, many examples of these on SO


In the days of CP/M, filesize was a number of 128-byte blocks. Since files were not often a multiple of 128 bytes long, convention was that a control-z marked the end-of-file. Backwards-compatibility is a big issue in computing as it's wasteful to revise existing systems to cater for a concept-revision. Hence, the control-Z convention is still recognised and observed for text files.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Hiya Mr Magoo, Thank you so much for your excellent code but I'm afraid I have no idea what you mean by: "Perform a directory scan. assigning each filename that matches the mask to %%b.". Could you explain what this means and how I might go about doing it, please? I believe your final comment about the ```COPY``` cmd was your way of explaining why I was getting an extra hex1A character at the end of some files; please correct me if I'm wrong on that point? I would like to show what I've done but there's no room here; is there a way to make a further "post" in this 'Thread'? – Highlander Jul 20 '22 at 18:00
  • Is it possible to add more simply by clicking on the "Answer your Question" button? – Highlander Jul 20 '22 at 18:02
  • Modifying questions once asked, except to clarify or provide requested information, is discouraged on SO as it can lead to a never-ending question far removed from the original title. It's possible to establish a chat room. You can always ask another question, of course. You can `edit` a question by pressing `edit` of the `Share Edit Follow Close Flag` buttons appearing at the bottom of the post. Yes - I'd agree that it would be helpful of these were surrounded by an oval to make them appear to be buttons... – Magoo Jul 21 '22 at 01:55
  • The `Answer your Question` facility is usually used to allow a solution to be published that has appeared to the OP through further research. – Magoo Jul 21 '22 at 01:55