Community
    • Login

    File name variable to replace text.

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    9 Posts 4 Posters 3.6k 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.
    • Ali namterA
      Ali namter
      last edited by Ali namter

      Hello all,

      Please, I have thousands of XML files and I would like to replace the value of a particular XML tag with the XML file name. Is there a file name variable I can use in the “Find in files” popup? If not, is there another way to do it?

      Thank you very much.

      PeterJonesP 1 Reply Last reply Reply Quote 0
      • PeterJonesP
        PeterJones @Ali namter
        last edited by

        @ali-namter ,

        No, there is not a “file name variable” for the replacement segment of the search/replace

        I know there have been discussions before in this forum that have shown alternative methods of inserting the filename in a particular place in the file…

        For example, this post shows a command you could run in a cmd.exe window in your XML folder, which would append the name of the XML to each XML file (of course using *.xml instead of *.txt from that example). And then you could use Find In Files to replace the placeholder value from your XML with the filename from the end of the file. If after the cmd.exe run, you had a file that looked like:

        <particular>PLACEHOLDER</particular>
        <other>...<other>
        filename.xml
        

        then your Find in Files FIND = (?<=<particular>)PLACEHOLDER(?=</particular>(?s:.*)(?-s:^(.*)\Z)) would put the end-of-file filename into group#1, but only replace PLACEHOLDER, and REPLACE = $1 would replace PLACEHOLDER with the filename from the end. Then a separate Find in Files with FIND = (?-s)^.*\Z and REPLACE = (empty) would delete the filenames from the end of the file. (yes, it would be possible to do that in one step instead of two, but the time to debug that combined regex compared to the time to run the two separate regex means it is not worth it, in my opinion)

        Alternatively, you could write a script for the PythonScript plugin which would go through all the XML files, open it, run a search-and-replace from Python which makes use of the fact that PythonScript has access to the active filename., and do the replacement that way, rather than using Find in File.

        Ali namterA 1 Reply Last reply Reply Quote 2
        • Ali namterA
          Ali namter @PeterJones
          last edited by

          @peterjones
          That’s quite beautiful. Thank you very much! As I am a newbie, I am however having a hard time designing the regex to use. If it’s not much to ask, can you please help me design one to replace the “abcdef” string with the filename present at the end of the file. There are a couple of id tags in the XML files but the one I am interested in is always present after the comment below. Thank you so much.

          …
          <!-- C.1.1: Report Unique Identifier -->
          <id extension=“abcdef” root=“2.16.840.1” />
          …

          PeterJonesP 1 Reply Last reply Reply Quote 0
          • PeterJonesP
            PeterJones @Ali namter
            last edited by

            @ali-namter ,

            Sorry, that was ambiguous.

            Does that mean that the one you want to replace will always have the exact text abcdef? Or does that mean that the old ID might be any value, but it will always come after a comment that matches the previous line exactly? Or does it mean something else?

            Ali namterA 1 Reply Last reply Reply Quote 0
            • Ali namterA
              Ali namter @PeterJones
              last edited by

              @peterjones No, it won’t have always the same value but it will always comme after the mentionned comment as you said.

              PeterJonesP 1 Reply Last reply Reply Quote 0
              • PeterJonesP
                PeterJones @Ali namter
                last edited by

                @ali-namter said in File name variable to replace text.:

                @peterjones No, it won’t have always the same value but it will always comme after the mentionned comment as you said.

                Okay, then FIND = (?<=<!-- C.1.1: Report Unique Identifier -->\r\n<id extension=")[^"]*(?="(?s:.*)(?-s:^(.*)\Z)) should do what you want.

                In the fake file

                <id extension="abcdef" root="2.16.840.1" />
                <!-- C.1.1: Report Unique Identifier -->
                <id extension="abcdef" root="2.16.840.1" />
                <particular>PLACEHOLDER</particular>
                <other>...<other>
                filename.xml
                

                the regex I gave will match the abcdef after the comment, not the one before. So after the REPLACE = $1, it will be:

                <id extension="abcdef" root="2.16.840.1" />
                <!-- C.1.1: Report Unique Identifier -->
                <id extension="filename.xml" root="2.16.840.1" />
                <particular>PLACEHOLDER</particular>
                <other>...<other>
                filename.xml
                

                Given your example data, that’s the best I can do.

                Ali namterA 1 Reply Last reply Reply Quote 2
                • Ali namterA
                  Ali namter @PeterJones
                  last edited by

                  @peterjones Fantastic. Thank you very much Peter. Very much appreciated. It works jut fine.

                  1 Reply Last reply Reply Quote 0
                  • guy038G
                    guy038
                    last edited by guy038

                    Hello, @ali-namter, @peterjones and All,

                    To @peterjones :

                    Peter, AFAIK, the allowed variables for the DOS for command are only the letters, in uppercase or lowercase and cetainly not the %0 to %9 parameters !

                    Thus, 52 cases are possible, from %A to %z !

                    So, I don’t understand why you and @terry-r used, for instance, the %1 parameter, like in here. I suppose that the right syntax should be, for instance :

                    For %A in (*.txt) do @echo @@ >> %A
                    for %z in (*.txt) do @echo %z >> %z
                    

                    To @ali-namter :

                    So, if your “thousand of XML files”, with the comment <!-- C.1.1: Report Unique Identifier -->, are all saved in a specific folder, here is a simple method, which is a @peterjones’s variant !

                    I assume that there is a single comment <!-- C.1.1: Report Unique Identifier --> line per .xml file

                    • Move to your specific folder

                    • Backup all your .xml files ( One never knows ! )

                    • Open a DOS command prompt

                    • Type the command for %i in (*.xml) do @echo %i >> %i and valid with Enter

                    => A new line containing the filename had been added at the very end of each .xml file and ends with a space char and the end-of-line chars

                    • Now, start Notepad++

                    • Open the Find in Files dialog

                      • SEARCH (?-i)(?<=<!-- C.1.1: Report Unique Identifier -->\r\n<id extension=")[^"\r\n]*(?s)(.*)^(.+)\x20\R

                      • REPLACE \2\1

                      • FILTERS *.xml

                      • DIRECTORY Your specific folder

                      • Select the Regular expression search mode

                      • If necessary, tick the In all sub-folders option

                      • Click on the Replace in Files button

                      • Click on the OK button of the Are you sure? dialog

                    => The different filenames should be inserted, instead of the abcdef string ( Note that an empty zone <id extension="", before replacement, is also a valid case )

                    => Each filename, at the very end of each .xml file, have been deleted, too !

                    Best Regards,

                    guy038

                    Terry RT 1 Reply Last reply Reply Quote 1
                    • Terry RT
                      Terry R @guy038
                      last edited by

                      @guy038 said in File name variable to replace text.:

                      So, I don’t understand why you and @terry-r used, for instance, the %1 parameter, like in here.

                      Well, I guess I’ve always done it that way and it’s always worked. See here:

                      826e4449-7594-4dd8-bdb5-db98cc7395c0-image.png

                      However, upon reflection (well some googling) I see Microsoft’s official documentation supports your statement. See here

                      So I stand corrected, yet why does the %1 thru %9 work then if Microsoft say those variables aren’t to be used in this context but within the batch file as batch parameters.

                      Terry

                      1 Reply Last reply Reply Quote 2
                      • PeterJonesP PeterJones referenced this topic on
                      • First post
                        Last post
                      The Community of users of the Notepad++ text editor.
                      Powered by NodeBB | Contributors