Community
    • Login

    adding the FileName into the file itself.

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    8 Posts 3 Posters 2.7k Views
    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.
    • Samir ZarourS
      Samir Zarour
      last edited by Samir Zarour

      hii. I have a lot of text files. (all are the same just have different numbers)
      I want to add the file name to this file…

      example: file name is new-test22.txt
      i want to add “new-test22” in this file…

      I try the “search and replace”…
      but I don’t know how to make it work.

      i try to search for “test-0” (my first line in all files).
      for here its all ok. but the “replace with”… WON’T work.

      i try:
      -filename
      $(FILE_NAME)

      did not work…

      PeterJonesP 1 Reply Last reply Reply Quote 0
      • PeterJonesP
        PeterJones @Samir Zarour
        last edited by

        @Samir-Zarour ,

        Sorry, the search/replace engine doesn’t have access to “metadata” like the active filename or directory or other such.

        It can easily be done in one of the scripting plugins – I know we’ve shown examples of such code in the past, but I’m not immediately finding one. If I do, I’ll post another reply to link you there.

        Samir ZarourS 1 Reply Last reply Reply Quote 0
        • Samir ZarourS
          Samir Zarour @PeterJones
          last edited by

          @PeterJones
          What i need to search? Exactly

          I’v been try from yesterday to find this out.

          PeterJonesP 1 Reply Last reply Reply Quote 0
          • PeterJonesP
            PeterJones @Samir Zarour
            last edited by

            @Samir-Zarour ,

            If I knew what to search, exactly, I would have found it already and posted. Have a bit of patience: you may have been having trouble with this for a day, but I’ve only known of your question for about 5 minutes.

            Samir ZarourS 1 Reply Last reply Reply Quote 0
            • Samir ZarourS
              Samir Zarour @PeterJones
              last edited by

              @PeterJones
              Thank you. 😊

              PeterJonesP 2 Replies Last reply Reply Quote 0
              • PeterJonesP
                PeterJones @Samir Zarour
                last edited by PeterJones

                @Samir-Zarour ,

                I haven’t been able to find the solution using the PythonScript plugin, which is what I was expecting to find. However, I did find https://community.notepad-plus-plus.org/post/39327 . In that post from 2019, @Terry-R explained a way to:

                1. Edit file %AppData%\Notepad++\shortcuts.xml, add the macro that Terry provided, and save that file
                2. Exit Notepad+ and restart Notepad++ (to get it to load that macro into the Macro menu)
                3. Open all the files that you want to edit
                4. Run the macro on all of the files (you could assign a keyboard shortcut to the macro, and then run that keyboard shortcut a bunch of times)
                  – this macro does the first step of inserting the filename at the end of each file, saving the file, and closing the file
                5. Then run a Search > Find in Files regular-expression search to move the filename from the end of the file to the right location in your file

                For dozens to maybe 100 files, that shouldn’t be too bad of a process.

                A couple of related posts, though they don’t provide a fully-Notepad++ solution to your exact problem
                https://community.notepad-plus-plus.org/topic/16686/renaming-title-tags-using-html-file-names
                https://community.notepad-plus-plus.org/topic/16217/how-to-insert-path-filename-into-the-notepad-text/4

                Alan KilbornA 1 Reply Last reply Reply Quote 1
                • PeterJonesP
                  PeterJones @Samir Zarour
                  last edited by

                  @Samir-Zarour ,

                  Actually, since you want to replace your entire first line, this version of the macro will delete the first line, then insert the filename as the first line, then save it and close it.

                  <Macro name="Replace First Line with Filename" Ctrl="no" Alt="no" Shift="no" Key="0">
                      <Action type="0" message="2316" wParam="0" lParam="0" sParam="" />
                      <Action type="0" message="2338" wParam="0" lParam="0" sParam="" />
                      <Action type="2" message="0" wParam="42030" lParam="0" sParam="" />
                      <Action type="0" message="2179" wParam="0" lParam="0" sParam="" />
                      <Action type="1" message="2170" wParam="0" lParam="0" sParam="\r" />
                      <Action type="1" message="2170" wParam="0" lParam="0" sParam="\n" />
                      <Action type="2" message="0" wParam="41006" lParam="0" sParam="" />
                      <Action type="2" message="0" wParam="41003" lParam="0" sParam="" />
                  </Macro>
                  

                  With that, I think you can skip step#5. (Unless you want to get rid of the filename’s extension, in which case you’ll need to then search for the right extension on the first line, then replace it with nothing)

                  1 Reply Last reply Reply Quote 2
                  • Alan KilbornA
                    Alan Kilborn @PeterJones
                    last edited by Alan Kilborn

                    Peter provided a macro, but looking at it, you can’t easily tell what it does (of course, Peter describes it, but what if he didn’t?).

                    I’ve been working on a macro “disassembler” that will help to reverse-engineer a provided macro, or even let you just recall what your own macros actually do.

                    Here’s what my in-progress tool produces for Peter’s macro:

                    <Macro name="Replace First Line with Filename" Ctrl="no" Alt="no" Shift="no" Key="0">
                        <Action type="0" message="2316" wParam="0" lParam="0" sParam="" />
                            SCI_DOCUMENTSTART
                        <Action type="0" message="2338" wParam="0" lParam="0" sParam="" />
                            SCI_LINEDELETE
                        <Action type="2" message="0" wParam="42030" lParam="0" sParam="" />
                            IDM_EDIT_FILENAMETOCLIP
                        <Action type="0" message="2179" wParam="0" lParam="0" sParam="" />
                            SCI_PASTE
                        <Action type="1" message="2170" wParam="0" lParam="0" sParam="\r" />
                            SCI_REPLACESEL
                        <Action type="1" message="2170" wParam="0" lParam="0" sParam="\n" />
                            SCI_REPLACESEL
                        <Action type="2" message="0" wParam="41006" lParam="0" sParam="" />
                            IDM_FILE_SAVE
                        <Action type="2" message="0" wParam="41003" lParam="0" sParam="" />
                            IDM_FILE_CLOSE
                    

                    It does require a bit of N++ “smarts” to know what things like SCI_REPLACESEL and IDM_EDIT_FILENAMETOCLIP mean, but it isn’t a real stretch of the imagination that even noobs could figure it out.

                    1 Reply Last reply Reply Quote 4
                    • First post
                      Last post
                    The Community of users of the Notepad++ text editor.
                    Powered by NodeBB | Contributors