0

How would I do this? Whenever I save a file, I would like Notepad++ to:

  1. Find a line in my header near the top of the file that looks like "DateTime: ${stuff}$" and insert the current date/time (short format aka CTRL-SHIFT-F4) between the DOLLARSIGNS.
  2. Find a line near the top that looks like "Change: ${number}$" and increment the integer value between the DOLLARSIGNS.
  3. THEN save the file.

The net result is that every time I use the "Save File" keystroke/action, the file has the current date/time, and the "Change" number gets bumped.

Before saving the file:

/*
   Author:     [email protected]$
   DateTime:   $$
   File:       $C:\Users\usr\source\sql\Role_Assignment_Details.sql$
  
   Change:     $1$
*/

After saving the file:

/*
   Author:     [email protected]$
   DateTime:   $2:42 PM 7/26/2024$
   File:       $C:\Users\usr\source\sql\Role_Assignment_Details.sql$
  
   Change:     $2$
*/

I don't even know where to begin.

2
  • Does it need to happen for the default Save button and keyboard-shortcut (Control+S), or having an alternate keyboard shortcut to do the saving routine would suffice? Commented Jul 29 at 20:16
  • 1
    @YisroelTech: Thanks for replying. I'd be happy with either option. Commented Jul 30 at 21:46

1 Answer 1

2

You can accomplish that by using the PythonScript plugin with a script.

  1. Install the PythonScript plugin from the Plugins Admin (Plugins>Plugins Admin)
  2. Create a new script file by going to Plugins>Python Script>New Script. When asked for a file name, name it something like update_file_header.py
  3. In the file that opens (the update_file_header.py file) paste the following Python script, and save it:
import re
from datetime import datetime
from Npp import editor, notepad, NOTIFICATION

def update_file_header(args):
    editor.beginUndoAction()
    
    try:
        text = editor.getText()
        
        # Update DateTime
        current_datetime = datetime.now().strftime("%I:%M %p %m/%d/%Y")
        date_time_pattern = re.compile(r'(DateTime:\s*\$)(.*?)(\$\s*\n)')
        text = date_time_pattern.sub(lambda m: m.group(1) + current_datetime + m.group(3), text)
        
        # Increment Change number
        def increment_change(match):
            current_change_number = int(match.group(2))
            new_change_number = current_change_number + 1
            return match.group(1) + str(new_change_number) + match.group(3)
        
        change_number_pattern = re.compile(r'(Change:\s*\$)(\d+)(\$\s*\n)')
        text = change_number_pattern.sub(increment_change, text)
        
        editor.setText(text)
    
    finally:
        editor.endUndoAction()

# Register the callback for the FILEBEFORESAVE event
notepad.callback(update_file_header, [NOTIFICATION.FILEBEFORESAVE])
  1. Open the PythonScript Configuration (Plugins>Python Script>Configuration), change Initialisation to ALLSTARTUP, and press OK to save
  2. Open the startup.py file (located in C:\Program Files\Notepad++\plugins\PythonScript\scripts, and add a line to the bottom of it import update_file_header, then save the file (it may require you to first restart Notepad as administrator and then save)
  3. Restart Notepad++

Then try to make a change in a sample file (with such a header) and see how it updates automagically.

See in action: enter image description here

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .