Systematic addition of text and the current file name into 320 files



  • I’ve seen a topic covering how to insert the filename as text through some mouse clicks. I have some 320+ HTML files that are generated using DocFlex. Unfortunately, this tool does not generate a <title> element. I need to add one to 300 files. I would rather not do that through a mouse click. Any suggestions?



  • @Aaron-Mills said in Systematic addition of text and the current file name into 320 files:

    how to insert the filename as text

    Perhaps read this thread which I was involved in some time ago. It could be a good starting point. You will likely need to alter some of the process to suit your needs, but at least it outlines the keystrokes (mouse) commands and it may help you solve the issue. By all means come back to this thread once you have read that with any questions.

    https://community.notepad-plus-plus.org/topic/17035/batch-function-need-to-add-filename-at-the-end-of-each-paragraph

    Terry



  • @Aaron-Mills - I use Windows command prompt’s built in scripting and GNU sed to do what you need. For example, start up command prompt and cd to the top directory of where your HTML files are. Run this command for /r %i in (*.htm) do @echo %~ni You will see a list with the file names for each *.htm files from the current directory on down.

    Let’s say you want to add a <title>file-name</title> element at the top of the <head> section of each file.

    for /r %i in (*.htm) do sed -i -r -e "s~(<head>)~\1\n<title>%~ni</title>~" "%i"
    del sed??????
    

    You asked for the file name. If you also want the file extension included in the title then use %~nxi
    If you want or need to use the full file path then it gets painful. While the full path is available using %~fi you can’t use it as-is in a regular expression as the path contains \ backslash characters. There are workarounds but that’s getting out of the scope of how to do this given my suggestion does not use Notepad++.

    The second line with del sed?????? removes the temporary files created by sed’s -i command line option which I used to do in-place editing of all the HTML files.


Log in to reply