search something and replace it with the file name of the file at hand
-
Using “find in files” I am trying to search for a string at txt files and replace that string with the file name of the file that is searched at that moment.
I have used the “$(FILE_NAME)” variable but notepad++ treats it as a string not as a variable and does not return the file name.
Thanks a lot in advance for you help.
-
@Christos-Glavopoulos-adjuster-GR said in search something and replace it with the file name of the file at hand:
replace that string with the file name of the file that is searched at that moment
I created a solution for a very similar problem sometime ago. Here is the link.
I’d suggest have a read of that, it will require some adjustments but give it a go. We are on hand to help if needed.
Terry
-
@Christos-Glavopoulos-adjuster-GR ,
The search/replace engine doesn’t have access to the “metadata” (including filename), sorry. You can see this similar conversation “adding the FileName into the file itself” for some workarounds.
other related posts that were mentioned in that conversation
- batch function - need to add filename at the end of each paragraph
- Renaming <title> tags using HTML file names
—
Just before posting, I see that Terry beat me to linking one of those topics.
But looking at Terry’s macro from that post – it’s still using the

and

in the macro string. As of v8.5.3, macros must use the shorter versions, so
and

– so you need to change the two lines<Action type="1" message="2170" wParam="0" lParam="0" sParam="
" /> <Action type="1" message="2170" wParam="0" lParam="0" sParam="
" />
to
<Action type="1" message="2170" wParam="0" lParam="0" sParam="
" /> <Action type="1" message="2170" wParam="0" lParam="0" sParam="
" />
-
@Christos-Glavopoulos-adjuster-GR
I realised that my old post, while it will assist (once edits such as @PeterJones suggested), has too many steps for your need. Since you only need to insert the filename in one position (or a small number of positions) the following will probably be a better idea. Please read the entire post before trying it as I may have made some assumptions which aren’t correct.
With a test file open in Notepad++ complete the following:
- Menu option Macro, then Start Recording.
- Ctrl + Home key. This places the cursor at the very start of the file.
- Use the Find function with the string/regex (see Note-1 below) to find the string. Click on Find Next, then click Close. So at this point the string you are searching for should be highlighted.
- Menu option Edit, then select Copy to Clipboard. At this point you need to identify which sub-option you want. It will most likely be “Copy Current Filename”, but you may want the first option “Copy Current Full File Path” which includes the path with the filename.
- Ctrl + V. Paste the clipboard content to the current cursor position.
- Ctrl + S then Ctrl + W. Save current file and then close current file.
- Menu option Macro, then Stop Recording.
Now if you need to replace a (small known) number of strings with the filename (in each file you are editing) you will need to record additional steps in the macro. They would fit after step #5 and be repeated for the number of times replacement is required, thus:
5b. Use the Find function with the string/regex to find the string. Click on Find Next, then click Close. Now this string/regex could be the same one, or a different one. If the 2nd string/regex to find is different you should record the Ctrl + Home shortcut first. This resets the cursor to the start of the file before opening the Find function. If the same string/regex, this is not required.
5c. Ctrl + V. As the filename is already in the clipboard, we only need to paste, not have to reload to clipboard.Give the macro a name (Save Current Recorded Macro) and possibly a shortcut key which allows you to run it without going to the menu. Be aware, a lot of shortcut combinations are already in use so you will likely have to compromise on what you’d prefer to use. Shortcut Mapper under Menu option Settings will show you what shortcuts are currently in use, just use the filter on each of the tabs and type something like “ctrl alt” or “shift alt”.
Note-1: I’ve referred to regex (regular expression) in this post. However the string you are searching for may be a static one (no variation across all instances) in which case the Find function would have the setting Search Mode set to Normal, rather than Regular Expression.
So once the Macro is recorded, you can load all the files that need to be edited. You need to know how many files. I’m not sure if there is an easy way to determine that within Notepad++, but one method is to Edit, Copy to Clipboard, All Filenames. Open a new tab and Ctrl + V, then Ctrl + End. Look at the number of lines created. Then close this tab and don’t save. Someone else may have a better approach (possibly a built-in Notepad++ function?), I had a quick look but didn’t see one.
To process all these files:
- Menu option Macro
- Run a Macro Multiple Times
- Select the macro saved above.
- Enter the number in the field next to “times”.
- Click “Run”.
Once the macro has finished there should be no tabs left open in Notepad++ unless you got the number wrong. Possibly open up a few of the edited files to verify the process worked as planned. You might even want to run the process over some test files first (make copies of the real ones). If this needs to be a repeatable process, having a macro will help.
If you are having problems come back to the forum. Remember to provide examples in the way the FAQ post FAQ: Template for Search/Replace Questions asks you to.
Terry
-
Thank you very much Terry R and PeterJones.
I have 2500 different php files and I want to add it’s individual file name at each one of them.
The position to add it, is already set in every php file as <ADD_HERE>
To these 2500 different php files, I want to search for the <ADD_HERE> and replace it with <link rel=“canonical” href=“<FILE_NAME>”>
I have never used macros. I can setup an excel file. The problem is how to connect each excel line to the correct file at hand and thus proceed with the replace.
Could you please give me a more simple solution than the ones you already mentioned.
Many thanks
Christos
-
@Christos-Glavopoulos-adjuster-GR said in search something and replace it with the file name of the file at hand:
Could you please give me a more simple solution than the ones you already mentioned.
Have you tried the solutions already given??
-
@Alan-Kilborn
What If I wanted to search for a specific number for example : 3XXXXX5849
XXX are Unknown digit numbers
thanks for help in advance. -
What If I wanted to search for a specific number for example : 3XXXXX5849
Why are you asking the same question in multiple threads? I’m -1ing this question because you did that.
-
@Alan-Kilborn Thanks for making me do :)
Yes I have tried the solution by @Terry-R and it worked like a charm.
Many Thanks to you and especially @Terry-R
Best regards from Thessaloniki, Greece
Christos
-
@Terry-R said in search something and replace it with the file name of the file at hand:
@Christos-Glavopoulos-adjuster-GR
With a test file open in Notepad++ complete the following:
- Menu option Macro, then Start Recording.
- Ctrl + Home key. This places the cursor at the very start of the file.
- Use the Find function with the string/regex (see Note-1 below) to find the string. Click on Find Next, then click Close. So at this point the string you are searching for should be highlighted.
- Menu option Edit, then select Copy to Clipboard. At this point you need to identify which sub-option you want. It will most likely be “Copy Current Filename”, but you may want the first option “Copy Current Full File Path” which includes the path with the filename.
- Ctrl + V. Paste the clipboard content to the current cursor position.
- Ctrl + S then Ctrl + W. Save current file and then close current file.
- Menu option Macro, then Stop Recording.
I have tried your solution @Terry-R and it worked like a charm.
Many many Thanks
Christos