Re: Filename extract and rename
Posted by richieclose on Feb 08, 2016; 9:40pm
URL: http://imagej.273.s1.nabble.com/Filename-extract-and-rename-tp5015526p5015575.html
Arragh - thought I was there! I've written the script below. And seems to work fine where there is one folder, but I'd like to check sub folders also, so,
Selected source folder-->subfolder 1
-->subfolder 2
-->subfolder 3
It looks like the script works if there is only one folder, but I get an error if there are multiple folders:
Row(2) out of range in line 32
setResult("File name", i, f_name<)>;
It looks like my index is not increasing after one file name has been processed... but not sure. Any help would be much appreciated.
dirs = getDirectory("Choose a Source Directory ");
dird = getDirectory("Choose a destination Directory");
setBatchMode(true);
run("Clear Results");
count = 0;
n = 0;
processFiles(dirs);
function processFiles(dirs) {
list = getFileList(dirs);
for (i=0; i<list.length; i++) {
if (endsWith(list[i], "/"))
processFiles(""+dirs+list[i]);
else {
showProgress(n++, count);
path = dirs+list[i];
processFile(path);
i++;
}
}
}
function processFile(path){
f_name = list[i];
if (endsWith(path, ".jpg"))
{
setResult("File name",i,f_name);
string1 = substring(f_name,25,26);
string2 = substring(f_name,27,33);
f_name = (string1+string2);
setResult("Time Stamp",i,f_name);
print(f_name);
}
}
setOption("ShowRowNumbers", false);
updateResults;
path = dird + "image_names.csv";
saveAs("Results", path);
if (isOpen("Log")) {
selectWindow("Log");
run("Close");
}
if (isOpen("Results")) {
selectWindow("Results");
run("Close");
}
showMessage("Image processing complete")