Community
    • Login

    Adding text to beginning and end of files for whole directory.

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    2 Posts 2 Posters 3.4k Views 1 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Andy LyA Offline
      Andy Ly
      last edited by

      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!

      1 Reply Last reply Reply Quote 0
      • Terry RT Offline
        Terry R
        last edited by Terry R

        @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. and last 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

        1 Reply Last reply Reply Quote 3

        Hello! It looks like you're interested in this conversation, but you don't have an account yet.

        Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

        With your input, this post could be even better 💗

        Register Login
        • First post
          Last post
        The Community of users of the Notepad++ text editor.
        Powered by NodeBB | Contributors