2

Is there a way to have Notepad++ generate new file names with the current date?

Like this: YYYY_MM_DD_new1.txt or similar. Currently it just names them: new1, new2, etc.

Date in the file name will work great with autosave, there will be no name conflicts after NPP restart.

All I want is a way to store sessions between restarts. I want to autosave even the unnamed files.

Thanks.

3
  • Is this for use with the AutoSave plugin or something similar? Jul 17, 2013 at 15:52
  • sublimetext saves sessions Jul 17, 2013 at 15:59
  • Yes, for use with autosave. I am not interested in sublimetext, thanks. Notepad++ saves sessions too, just doesn't assign unique filenames.
    – vt.
    Jul 17, 2013 at 17:36

3 Answers 3

3

I just did this using the Python Script plugin for NPP...

notepad.clearCallbacks([NOTIFICATION.BUFFERACTIVATED])
def my_callback(args):
    if notepad.getBufferFilename(args["bufferID"]) == "new  1":
        fmt = '%Y%m%d%H%M%S'
        d = datetime.datetime.now()
        d_string = d.strftime(fmt)
        notepad.saveAs('X:\\Documents\\Notepad++_autosave\\%s.txt' % d_string)
notepad.callback(my_callback, [NOTIFICATION.BUFFERACTIVATED])

With the above code, as soon as I type Ctrl+N, the new file is created and saved instantly with the name formatted as defined in 'fmt' above. The path for the file to be saved is defined above as well; change it to suit your environment.

2
  • As of v6.7.9.2 of NPP, the single-quote for 'new 1' in the 'if' test fails, but replacing the single-quote with double-quote restored functionality.
    – mwoliver
    Jul 13, 2015 at 20:21
  • Great. I modified condition to: notepad.getBufferFilename(args["bufferID"])[0:4] == 'new ': and filename: newFilename = ('%s\\%s.txt' % (os.environ['TEMP'], d_string)) Also put this to startup.py
    – vt.
    Feb 27, 2017 at 19:58
1

Notepad ++ has a plugin Autosave2 that saves your documents with a time stamp in the file name like this :

ToDo.txt.2013-04-19 10.13.09

ToDo.txt.2013-04-19 10.14.09

ToDo.txt.2013-04-19 10.15.09

4
  • From the Autosave2's page: The above settings will create a new backupfile for "ToDo.txt" in D:\NPP_\Backups as follows (if the text has changed within 1 minute): ToDo.txt.2013-04-19 10.13.09 ToDo.txt.2013-04-19 10.14.09 ToDo.txt.2013-04-19 10.15.09. I don't want extra copies of the file though, I want the file itself to have a unique name.
    – vt.
    Jul 17, 2013 at 17:34
  • aah, my bad. But your requirements seem to specific, I guess you will have to get someone to make a plugin that does it for you.
    – Shekhar
    Jul 17, 2013 at 18:05
  • 1
    I created it. Didn't even think of that originally.
    – vt.
    Jul 17, 2013 at 21:33
  • 1
    hey great job, I think you should publish a link to your plugin here and mark it as the asnwer
    – Shekhar
    Jul 17, 2013 at 23:20
0

I tried using mwoliver's answer and still had trouble running it. I made some changes and now, this will work for any "new #" format instead of just "new 1".

notepad.clearCallbacks([NOTIFICATION.BUFFERACTIVATED])
def my_callback(args):
    set1 = set(notepad.getBufferFilename(args["bufferID"]).split(' '))
    filenew = set1.pop()
    filenumber = set1.pop().isdigit()
    setempty = len(set1) == 0
    if filenew == "new" and filenumber and setempty:
        fmtdate = '%Y-%m-%d'
        fmttime = '%H%M%S'
        d = datetime.datetime.now()
        d_string = d.strftime(fmtdate) + ' Notes ' + d.strftime(fmttime)
        notepad.saveAs('C:\\Users\\username\\Desktop\\%s.txt' % d_string)
notepad.callback(my_callback, [NOTIFICATION.BUFFERACTIVATED])

I also added the following snippet to the end of the startup.py file (after the code above) in order to prevent the "new 1" upon notepad++'s startup.

if notepad.getCurrentFilename() == 'new 1':
    notepad.new()
    notepad.activateIndex(0,0)
    if notepad.getCurrentFilename() == 'new 1':
        notepad.close()

Not the answer you're looking for? Browse other questions tagged or ask your own question.