adding the FileName into the file itself.
-
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…
-
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.
-
@PeterJones
What i need to search? ExactlyI’v been try from yesterday to find this out.
-
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.
-
@PeterJones
Thank you. 😊 -
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:
- Edit file
%AppData%\Notepad++\shortcuts.xml
, add the macro that Terry provided, and save that file - Exit Notepad+ and restart Notepad++ (to get it to load that macro into the Macro menu)
- Open all the files that you want to edit
- 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 - 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 - Edit file
-
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)
-
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
andIDM_EDIT_FILENAMETOCLIP
mean, but it isn’t a real stretch of the imagination that even noobs could figure it out.