lördag 20 oktober 2012

Simple version control of your files

This litle script under the action menu in dolphin comes in handy when you often need to do copies of some files in one folder.
I use it when i am scripting and need to know that different script(files) are in sync with each other. It adds date and time to the end of the filename--20121020-2236.

Save this to a file named myAction.desktop ( the .desktop is the important part of the name ) in this folder  /home/USER/.kde/share/kde4/services/ . Changeg USER to  your username.
[Desktop Entry]
Type=Service
ServiceTypes=KonqPopupMenu/Plugin
MimeType=all/allfiles
Actions=backupFile

[Desktop Action backupFile]
Name=Copy file(s) and add date-time to filename(s) 
Icon=background
Exec=~/bin/cpRename.sh %F


Then in /home/USER/bin folder  save this to a file named cpRename.sh and make it executable.
#!/bin/bash
 
addDate=$(date +%Y%m%d) #20121020
addTime=$(date +%k%M) #1743
addText=$(kdialog --inputbox "Add additional text ( optional )")
isCansled=$? # Check if text was added or if the dialog was cancelled

# $@ is all the selected files and folders
for file in "${@}"
do
  # checks if it is a file so that not a folder is selected by mistake
  if [ -f "$file" ]
  then
    if [ $isCansled == 1 ]
      then
      new_name="$file--$addDate-$addTime"
      cp "$file" "$new_name"
    else
      new_name="$file--$addDate-$addTime-$addText"
      cp "$file" "$new_name"
    fi
  fi
done


Now restart Dolphin and select some files, right click and select actions and now ther should be a submenu with the name "Copy file(s) and add date-time to filename(s)"

Inga kommentarer: