Search code examples
batch-filebulk

How to bulk add text to beginning and ending of multiple files?


I have subtitle files (.srt). I want to add some texts to beginning and ending of these files. They all are not in same folder. So it must effect to subfolders files,too. Is there a batch code or any other way to do this?

EDIT : This codes helped me to add text to beginning of files. How can I change it for adding to ending of files?

@echo off
for /r %%a in (*.srt) do (

echo 'text' > "%%a.tmp"
type "%%a" >> "%%a.tmp"
del "%%a"
move "%%a.tmp" "%%a"

)

Solution

  • It seems you are not familiar at all and have not attempted to understand the script at all...

    for /recursive directory search after * (=all) .srt-files do (
    echo text >(=into) %%a(=original filename as parameter of recursive loop).tmp
    type >> %%a.tmp (echo whole content of file to the end of .tmp-file)
    delete old file
    move .tmp-file to old filename

    So the line you are using to add text to the top is echo 'text' > %%a.tmp before typing the current content into it.

    To add text after just move the same line after typing the current content. Additionally you will have to change > to >> as > means overwrite the file with said text and >> means append.