backup...



  • 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?



  • 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



  • 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])



  • 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



  • 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])


  • 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


Log in to reply