Sublime Forum

Save file as ISO date

#1

I would like a command to save a file as “{ISO Date}.md” (i.e., today would be “2012-06-23.md”) in a specific folder (always the same) without being prompted about anything. I can sort of see how one would go about this, but it’s beyond my ken. Any ideas?

(In an ideal universe, this command would not callously overwrite an existing file, but would helpfully disambiguate between them by tacking on a letter of the alphabet at the end — e.g., “2012-06-23-a.md”)

I often use Sublime to jot down things I don’t want to forget (usually when I’m doing two other, more important things). I would like to file this random assortment of information chronologically (hence the filename). And if that recall methodology fails, I can just grep the folder :smile:

Any help would be very appreciated.

0 Likes

#2

EDIT: removed old code excerpt

0 Likes

#3

That should give you a start, though you are going to want to handle cases of the file path already existing etc.

0 Likes

#4

Actually, you don’t even need that redundant writing of the buffer before saving, or even dumping to the file.

0 Likes

#5

[code]import sublime_plugin
import datetime
import os

class IsoNotes(sublime_plugin.TextCommand):
notes_path = r"C:\Users\nick\Desktop\notes"
date_format = r"%Y-%m-%d"

def run(self, edit):
    time = datetime.datetime.now().strftime(self.date_format)
    
    def suffixes():
        yield ''
        for i in xrange(2, 2**30):
            yield str(i)

    for suffix in suffixes():
        note_path = os.path.join(self.notes_path, time)
        note_path +=   '-' + suffix + '.md'
        if not os.path.exists(note_path):
            break

    self.view.retarget(note_path)
    self.view.run_command('save')[/code]
0 Likes

#6

@castles Where did you get retarget() from? There seems to be quite a number of API methods that aren’t in the docs.

Andy.

0 Likes

#7

Trust grep and introspection, not the docs :smile:

I saw it in the file renaming plugin in the Default package iirc

0 Likes

#8

Awesome! Thank you @castles

I’ve been tinkering with this plugin as you’ve been posting updates. From my rather quick testing I prefer the 2nd version because it was somewhat more robust (after I added a check not to overwrite an existing file). The 3rd version has more potential but is more finicky. But I will likely be able to spin out a command to save backups (e.g., title-1, title-2, etc.). Alas, I have yet to sit down with a proper versioning system.

I was also wondering about retarget()…

Thanks again!

Alex

0 Likes

#9

Appending to the days file perhaps?

0 Likes

#10

I’m not sure I understand your question.

I’m thinking of a different situation. I work a lot with text files (plain text or markdown, not code). I typically call them “some-file-6.md”. I change the file name by an ever increasing number just before I make an edit where I remove/restructure/lose information. It would be handy to have a command to do just that, but I think I will probably work out how to do on my own.

If you have another few minutes, is there a simple fix for the first file being saved as “{date}.md” rather than “{date}-.md” (that is,without the dash)? I’ve had a quick look, but couldn’t work that out.

I just re-read your question. Do you mean opening a new file, which, when saved, is then appended to the previous file? This might be handy if doable (and if it was what you meant) for editing the Monthly files. (Yes, I have monthly files, too. My system is a shambles :smile: )

Edit: Nevermind on the fix for the dash. I worked it out.

0 Likes