• Login
Community
  • Login

backup...

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
6 Posts 2 Posters 7.4k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A
    Andrew Yushkov
    last edited by Mar 4, 2016, 5:21 PM

    now backup path
    C:\Dropbox\Notepad++\

    and np++ creating files after save
    C:\Dropbox\Notepad++\test.txt.2016-03-04_201035.bak

    need
    C:\Dropbox\Notepad++#FILE_PATH##FILE_NAME#

    C:\Dropbox\Notepad++\var\www\test.txt.2016-03-04_201035.bak

    how to do such a thing?

    C 1 Reply Last reply Mar 4, 2016, 8:42 PM Reply Quote 0
    • C
      Claudia Frank @Andrew Yushkov
      last edited by Claudia Frank Mar 4, 2016, 8:43 PM Mar 4, 2016, 8:42 PM

      Hello @Andrew-Yushkov,

      afaik this isn’t possible to configure within npp at the moment.
      What I can offer is a python script which could, more or less, handle it automatically.

      from os import mkdir, rename
      from os.path import split, exists
      from glob import glob 
      
      main_backup_dir = 'PATH_TO_YOUR\\backup\\'
      new_directory = 'new\\' 
      
      def callback_FILESAVED(args):
          console.show()                                                  # open python script console
      
          _file = notepad.getBufferFilename(args['bufferID'])             # returns the full path of the currently saved document
          dir_part, file_part = split(_file)                              # split the path into directory part and file part
      
          f_bck = glob(main_backup_dir + '{0}.*.bak'.format(file_part))   # retrieve the file from the backup directory as list
          dir_part, file_part = split(f_bck[0])                           # split the path into directory part and file part of first item in list
          
          if exists(main_backup_dir + new_directory) == False:            # check if backup directory structure already exists
              mkdir(main_backup_dir + new_directory)                      # it doesn't, let's create it
          rename(f_bck[0], main_backup_dir + new_directory+file_part)     # move backup file, by renaming it, to new backup directory
          
          
          console.write('file backed up: {0}\n'.format(main_backup_dir + new_directory+file_part)) # just to see that it has been backed up
      
      notepad.clearCallbacks([NOTIFICATION.FILESAVED])
      notepad.callback(callback_FILESAVED, [NOTIFICATION.FILESAVED]) 
      

      The script assumes that only one backed up version of a file is in the backup directory, so for the first use you should, maybe,
      empty the backup directory.

      The opening question, I assume, is the directory structure to which a file belongs.

      As you see, there are two split functions, the first splits the current document the latter the backed up one. It splits the
      path into a directory part and into a file part, so, in theory, we could get the directory from the current path and add it to the
      new path. As I assume that you are working on windows and the files reside on a disk the dir_part would always start
      as, e.g. c:\ or d:\ which means, we could get the remaining part of path with

      dir_part[3:]
      

      which then could be added/used to/as new_directory

      If this should be automatically done, the script needs to be called startup.py and python script plugin configuration
      needs to be changed to ATSTARTUP

      If there is still something unclear, let me know.

      What needs to be done first is described here .
      Just in case that you haven’t installed python script plugin yet, I would propose to use the MSI package instead of using the plugin manager.

      Cheers
      Claudia

      1 Reply Last reply Reply Quote 0
      • A
        Andrew Yushkov
        last edited by Andrew Yushkov Mar 9, 2016, 4:50 PM Mar 9, 2016, 4:48 PM

        thanks=)

        from os import mkdir, rename
        from os.path import split, exists
        from glob import glob
        from datetime import datetime
        from shutil import copyfile
        import time

        main_backup_dir = ‘c:\np\’;

        def callback_FILESAVED(args):
        _file = notepad.getBufferFilename(args[‘bufferID’]) # returns the full path of the currently saved document
        dir_part, file_part = split(_file) # split the path into directory part and file part
        path = dir_part.split(“\”)
        from_server = False;
        for folder in path:
        if folder == ‘var’ or folder == ‘etc’:
        from_server = True
        sub_path = main_backup_dir;
        if from_server == True:
        sub_path += folder+“\”;
        if exists(sub_path) == False:
        mkdir(sub_path)
        if from_server == True:
        sub_path += datetime.now().strftime(‘%Y%m%d’)+“\”;
        if exists(sub_path) == False:
        mkdir(sub_path)
        copyfile(_file,sub_path+file_part+“.”+str(int(time.time()))+“.bak”)

        notepad.clearCallbacks([NOTIFICATION.FILESAVED])
        notepad.callback(callback_FILESAVED, [NOTIFICATION.FILESAVED])

        1 Reply Last reply Reply Quote 0
        • C
          Claudia Frank
          last edited by Mar 9, 2016, 5:03 PM

          Nice ;-) but why do you use if from_server == True twice?
          Btw. if you indent the whole code by at least 4 spaces the structure gets keeped.

          Cheers
          Claudia

          1 Reply Last reply Reply Quote 0
          • A
            Andrew Yushkov
            last edited by Mar 10, 2016, 6:00 AM

            1st from_server - search path on server in loop
            2nd from_server - copy file

            from os import mkdir, rename
            from os.path import split, exists
            from glob import glob
            from datetime import datetime
            from shutil import copyfile
            import time
            
            main_backup_dir = 'c:\\np\\';
            
            def callback_FILESAVED(args):
                _file = notepad.getBufferFilename(args['bufferID'])             # returns the full path of the currently saved document
                dir_part, file_part = split(_file)                              # split the path into directory part and file part
                path = dir_part.split("\\")
                from_server = False;
                for folder in path:
                  if folder == 'var' or folder == 'etc':
                    from_server = True
                    sub_path = main_backup_dir;
                  if from_server == True:
                    sub_path += folder+"\\";
                    if exists(sub_path) == False:
                      mkdir(sub_path)
                if from_server == True:
                  sub_path += datetime.now().strftime('%Y%m%d')+"\\";
                  if exists(sub_path) == False:
                    mkdir(sub_path)
                  copyfile(_file,sub_path+file_part+"."+str(int(time.time()))+".bak")
            
            notepad.clearCallbacks([NOTIFICATION.FILESAVED])
            notepad.callback(callback_FILESAVED, [NOTIFICATION.FILESAVED])
            
            1 Reply Last reply Reply Quote 0
            • C
              Claudia Frank
              last edited by Mar 10, 2016, 8:26 AM

              I see, formatted code - better understanding ;-)
              I put everything under the for loop so this didn’t quite make sense.
              Jfyi, there is also a os.makedirs function which could create the structrue in one go.

              Cheers
              Claudia

              1 Reply Last reply Reply Quote 0
              2 out of 6
              • First post
                2/6
                Last post
              The Community of users of the Notepad++ text editor.
              Powered by NodeBB | Contributors