7/30/2009

nautilus script : backup script

This script copy the selected files in the folder named 'old'.
If the folder is not exist, the script create 'old' folder.
Time stamp strings, e.g. "_Thu_Jul_30_11.05.39_2009", are automatically attached at the end of the file name.


#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, glob, time

old = 'old' # backup folder name

def showme(s):
os.system('gmessage -center ' + str(s))

def make_time_str():
tmp = time.ctime()
tmp = tmp.replace(' ', '_')
tmp = tmp.replace(':', '.')
return tmp

def make_old_folder():
if len(glob.glob(old)) == 1 and os.path.isdir(old) :
pass #
else :
os.system('mkdir old')

return

make_old_folder()

# get selected file paths
tmp = os.environ['NAUTILUS_SCRIPT_SELECTED_FILE_PATHS']
fns = tmp.strip().split('\n') #filenames

for fn in fns : # for filename in filenames
if os.path.isdir(fn) :
#showme('pass')
pass
fn_nameonly = os.path.basename(fn) # extract file name from file path
#showme(os.path.splitext(fn)[1])
cmd = 'cp ' + '"' + fn_nameonly + '" old/"' + fn_nameonly + '_' + make_time_str() + os.path.splitext(fn)[1] + '"'
os.system(cmd)

The script file must be in ~/.gnome2/nautilus-scripts/

댓글 없음: