Adding text to beginning and end of files for whole directory.
-
Hello,
I want to be able to add text to the beginning of the txt files and the end of it for the whole directory in bulk. There isn’t any type of common beginning or end to these communications that would allow me to find and replace.
Does anyone have a solution for this? Thank you!
-
@Andy-Ly said:
add text to the beginning of the txt files and the end of it for the whole directory in bulk
Welcome to the community.
It can be done simply with Notepad++. The regex (regular expression) I’ve provided should be copied into the appropriate fields within “Find in Files” function. This can be found under the main menu option “Search”.Find What:
^(?s).+
Replace With:line 1\r\n$0\r\nlast line
Filters: make this*.txt
and the Directory should be where your group of files to edit are located. As a suggestion either copy a few select files to another location and test first, or make sure you have a complete copy of ALL the files before editing. And after performing these steps you should verify a few random files to be sure it has worked as planned.Note
line 1
should be changed in the regex to the characters you wish to insert on the first line. andlast line
should also be changed to what you intend inserting at the end.To give some background the
(?s)
means that the.
character will capture ALL characters including line feed/carriage returns regardless of what the setting is on the “Find in Files” option. matches newline
.
The.+
then allows it to capture the entire file contents.
In the Replace With field first the new insertion is provided, then a newline (\r\n
), then we return the captured contents of the file `$0 and finally add a newline and the new last line to be inserted.You did not mention if the first and last lines were to be
new lines
, but my regex makes it so with the use of\r\n
. If you do NOT wish this, remove the 2 instances of\r\n
before preceding with the changes.Please note that the “search Mode” MUST be “regular expression”. After hitting the “Replace in Files” you may be presented with a question, this allows you to stop if you did NOT mean to apply the changes. If you continue all the files will be updated.
Check your results on a sample group of files before proceeding with the entire lot.
Let us know how it went, if there were any issues etc. We may be able to further fine tune the solution if required but would need more information.
Terry