Sunday, 6 February 2011

Directory Management with KRename

Yesterday, I got a very interesting e-mail from Todd about using KRename. His request required one new feature which was added to KRename SVN and makes KRename even more powerful. So, I think his usecase is interesting enough to be shown here.

First of all, he has several files which he wants to sort into different directories based on parts of their names. If the filename contains the word "essay" it is supposed to go into a subdirectory called "essay/" and the same should be done for the memo's. His example list of files looks like this:


Work Essay for fred.txt
Work Essay for bob.odt
Work Essay for alice.doc
Work Memo for mary.odt
Work Memo for ben.txt
Work Memo for carey.doc


At the end, we want a directory and file structure like this:

Essay/Work Essay for fred.txt
Essay/Work Essay for bob.odt
Essay/Work Essay for alice.doc
Memo/Work Memo for mary.odt
Memo/Work Memo for ben.txt
Memo/Work Memo for carey.doc



You can create directories in KRename during renaming of files using the [dirsep] operator or by simply using a / (slash) in the template. So, the template newdir[dirsep]$ will create a new directory called newdir and move all files to this directory. The token $ is KRename' way of saying, "insert the original filename here".

Now, one can combine this feature with the powerful regular expressions. Just go to the "Search and Replace ..." dialog and enter the regular expression Work ([\w]+) for and replace it with \1[direp]Work \1 for. The backreference \1 inserts a matched string from the regular expression into the results. Thereby, we can include either "memo" or "essay" in the new directory name. The matched part is indicated by brackets in the regular expression.

To make this work, one new feature was added to KRename. The dialog contains a new checkbox which allows to enable processing of KRename tokens in the replacement string of find and replace. We need this feature to process the [dirsep] token correctly and create a new directory
See the screenshots below.







If you have similar interesting usecases for KRename or questions on how to do thinks, do not hesitate to write a mail to our mailinglist!