Advertisement
Flynny85

NotepadBackUp

Jul 12th, 2016
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.05 KB | None | 0 0
  1.       ##########################################################################################################################################
  2.     ##      Notepad like backup : A basic no frills backup solution for SublimeText 3, all files will be                                        ##
  3.     ##      renamed with the addition of a time stamp and .bak and then saved to a location, No extra folders are created.                      ##
  4.     ##                                                                                                                                          ##                                                         
  5.     ## instructions to use:                                                                                                                     ## 
  6.     ##      Make sure to replace the _user_backup_path with your own file location you wish to save all your backups.                           ##
  7.     ##      Place this file in : C:/Users/UserName/AppData/Roaming/Sublime Text 3/Packages - if on windows.. or                                 ##
  8.     ##      use goto in Sublime Preferences/Browse Packages..                                                                                   ##
  9.     ##      it [i]SHOULD[i] run automagicaly in the background every time you save                                                              ##
  10.     ##      if not, To make sure this runs every time you save will need to run this command once in the console:                               ##
  11.     ##          window.run_command("NotepadBackUp")                                                                                             ##
  12.     ##                                                                                                                                          ##
  13.        
  14. import sublime
  15. import sublime_plugin
  16. import sys
  17. import os
  18. import time
  19. import shutil
  20.  
  21. class NotepadBackUpCommand(sublime_plugin.EventListener):
  22.  
  23.     def on_post_save(self, view):
  24.         _file = view.file_name()
  25.         _filename = (  os.path.basename( _file ) )
  26.         _filetime = time.strftime( "%Y_%m_%d_%H%M")
  27.         _filetime = ( _filetime[0:(len(_filetime) -1)]) ## limits to the 10 minute range eg wont write 00 - 60 but will write 0 - 6 versions..
  28.         _user_backup_path = "C:/Users/YourPathHereEtc .."
  29.         _filepath_time_bakup = ( _user_backup_path + '/' + _filename + '.' + _filetime + '.bak' )
  30.        
  31.         ExternalPathExists = os.path.exists( os.path.dirname( _filepath_time_bakup ) )
  32.         if ExternalPathExists is True:
  33.             shutil.copyfile( _file , _filepath_time_bakup )
  34.             sys.stdout.write('# ' + 'Creating Backup:- ' + _filepath_time_bakup + ' #\n')
  35.         else:
  36.             sys.stdout.write('# ' + 'ERROR (path does not exist??!) :- ' + _filepath_time_bakup + ' #\n')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement