Home Download Buy Blog Forum Support

Save file as ISO date

Save file as ISO date

Postby quodlibet on Sat Jun 23, 2012 2:35 pm

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 :)

Any help would be very appreciated.
quodlibet
 
Posts: 498
Joined: Sat Jun 18, 2011 5:53 pm

Re: Save file as ISO date

Postby sublimator on Sat Jun 23, 2012 3:04 pm

EDIT: removed old code excerpt
It is better to remain silent and be thought a fool, than to speak out and remove all doubt
sublimator
 
Posts: 648
Joined: Thu Mar 20, 2008 5:41 am

Re: Save file as ISO date

Postby sublimator on Sat Jun 23, 2012 3:05 pm

That should give you a start, though you are going to want to handle cases of the file path already existing etc.
It is better to remain silent and be thought a fool, than to speak out and remove all doubt
sublimator
 
Posts: 648
Joined: Thu Mar 20, 2008 5:41 am

Re: Save file as ISO date

Postby sublimator on Sat Jun 23, 2012 3:15 pm

Actually, you don't even need that redundant writing of the buffer before saving, or even dumping to the file.
It is better to remain silent and be thought a fool, than to speak out and remove all doubt
sublimator
 
Posts: 648
Joined: Thu Mar 20, 2008 5:41 am

Re: Save file as ISO date

Postby sublimator on Sat Jun 23, 2012 3:22 pm

Code: Select all
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')
It is better to remain silent and be thought a fool, than to speak out and remove all doubt
sublimator
 
Posts: 648
Joined: Thu Mar 20, 2008 5:41 am

Re: Save file as ISO date

Postby agibsonsw on Sat Jun 23, 2012 3:29 pm

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

Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Save file as ISO date

Postby sublimator on Sat Jun 23, 2012 3:42 pm

Trust grep and introspection, not the docs :)

I saw it in the file renaming plugin in the Default package iirc
It is better to remain silent and be thought a fool, than to speak out and remove all doubt
sublimator
 
Posts: 648
Joined: Thu Mar 20, 2008 5:41 am

Re: Save file as ISO date

Postby quodlibet on Sat Jun 23, 2012 3:45 pm

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
quodlibet
 
Posts: 498
Joined: Sat Jun 18, 2011 5:53 pm

Re: Save file as ISO date

Postby sublimator on Sat Jun 23, 2012 3:53 pm

Appending to the days file perhaps?
It is better to remain silent and be thought a fool, than to speak out and remove all doubt
sublimator
 
Posts: 648
Joined: Thu Mar 20, 2008 5:41 am

Re: Save file as ISO date

Postby quodlibet on Sat Jun 23, 2012 4:20 pm

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 :) )

Edit: Nevermind on the fix for the dash. I worked it out.
quodlibet
 
Posts: 498
Joined: Sat Jun 18, 2011 5:53 pm


Return to General Discussion

Who is online

Users browsing this forum: rusa13 and 105 guests