I've been working with exiftool a lot lately. One of the things I like about it is the way it can convert file names. If you've done any C programming, it's basically a sprintf-technique.
This function makes it brain-dead easy to get the base and extension of a filename
Code: Select allvar base = file.strf("%f");
var ext= file.strf("%e")
change a file's extension
Code: Select allvar fname = file.strf("%d/%f.psd");
and create completely new path names based on the old one
Code: Select allvar fname = file.strf("%d/%e/%f.%e);
I do stuff like this manually on a regular basis. Now there's a way to do it automagically.
Let me know if you find any problems.
Enjoy!
-X
Code: Select all//
// File.strf
// This is based of the file name formatting facility in exiftool. Part of
// the description is copy directly from there. You can find exiftool at:
// http://www.sno.phy.queensu.ca/~phil/exiftool/
//
// Description:
//
// Format a file string using a printf-like format string
//
//
// fmt is a string where the following substitutions occur
// %d - the directory name (no trailing /)
// %f - the file name without the extension
// %e - the file extension without the leading '.'
// %% - the '%' character
//
// if fs is true the folder is in local file system format
// (e.g. C:\images instead of /c/images)
//
// Examples:
//
// Reformat the file name:
// var f = new File("/c/work/test.jpg");
// f.strf("%d/%f_%e.txt") == "/c/work/test_jpg.txt"
//
// Change the file extension
// f.strf("%d/%f.psd") == "/c/work/test.psd"
//
// Convert to a file name in a subdirectory named after the extension
// f.strf("%d/%e/%f.%e") == "/c/work/jpg/test.jpg"
//
// Change the file extension and convert to a file name in a subdirectory named
// after the new extension
// f.strf("%d/psd/%f.psd") == "/c/work/psd/test.psd"
//
// Advanced Substitution
// A substring of the original file name, directory or extension may be
// taken by specifying a string length immediately following the % character.
// If the length is negative, the substring is taken from the end. The
// substring position (characters to ignore at the start or end of the
// string) may be given by a second optional value after a decimal point.
//
// For example:
//
// var f = new File("Picture-123.jpg");
//
// f.strf("%7f.psd") == "Picture.psd"
// f.strf("%-.4f.psd") == "Picture.psd"
// f.strf("%7f.%-3f") == "Picture.123"
// f.strf("Meta%-3.1f.xmp") == "Meta123.xmp"
//
File.prototype.strf = function(fmt, fs) {
var self = this;
var name = self.name;
var m = name.match(/\.([^\.]+)/);
var e = m ? m[1] : '';
m = name.match(/(.*)\.[^\.]+/);
var f = m ? m[1] : name;
var d = (fs ? self.parent.fsName : self.parent.toString());
var str = fmt;
if (false) {
// simple substitution
var str = str.replace(/%d/g, d);
str = str.replace(/%f/g, f);
str = str.replace(/%e/g, e);
str = str.replace(/%%/g, '%');
}
// advanced substition
var rex = /([^%]*)%(-)?(\d+)?(\.\d+)?(%|d|e|f)(.*)/;
while (m = rex.exec(str)) {
var pre = m[1];
var sig = m[2];
var len = m[3];
var ign = m[4];
var typ = m[5];
var post = m[6];
var subst = '';
if (typ == '%') {
subst = '%';
} else {
var s = '';
switch (typ) {
case 'd': s = d; break;
case 'e': s = e; break;
case 'f': s = f; break;
}
var strlen = s.length;
if (len || ign) {
ign = (ign ? Number(ign.substr(1)) : 0);
if (len) {
len = Number(len);
if (sig) {
subst = s.substr(strlen - len - ign, len);
} else {
subst = s.substr(ign, len);
}
} else {
if (sig) {
subst = s.substr(0, strlen-ign);
} else {
subst = s.substr(ign);
}
}
} else {
subst = s;
}
}
str = pre + subst + post;
}
return str;
};
File renaming code
File renaming code
was looking how to rename files and just found this.
thanks again!
thanks again!