Login  Register

Re: changing filenames

Posted by Jerome Mutterer-3 on Apr 03, 2011; 4:56pm
URL: http://imagej.273.s1.nabble.com/changing-filenames-tp3685166p3685169.html

Devrim,
here's another solution using the split function. You can specify padding
length for both the first and the second parts in your filename motives.

   requires ('1.45d'); // for IJ.pad function

print (changeFileNames2('1_2_xxx_yyy',3,3));


function changeFileNames2(s,length1,length2) {

  parts = split(s,'_');

  return
''+IJ.pad(parseInt(parts[0]),length1)+'_'+IJ.pad(parseInt(parts[1]),length2);

}

///


Sincerely,


Jerome



On Sun, Apr 3, 2011 at 2:32 PM, Devrim Pesen Okvur
<[hidden email]>wrote:

>  Jerome,
>
> Shortening the file name works. Thank you. (I placed the relevant line
>  with s to path into the loop in the macro I had)
>
> If the filename is 1_1_xxx_yyy how can it be changed to 1_01_xxx_yyy?
> (There are more than 10 files so the ordering works when they are
> numbered 1_01..., 1_02..., etc.)
>
> I appreciate your help.
>
>
> On Sun, Apr 3, 2011 at 10:50 AM, Jerome Mutterer
> <[hidden email]> wrote:
> > Devrim,
> >
> > you need to keep the left part of the file name, extending to the second
> > underscore, then pad the numeric part that extends to the first
> underscore
> > to 3 chars, and finally combine the padded index with the remaining part.
> > This translates in the following macro function :
> >
> > requires ('1.45d'); // for IJ.pad function
> > print (changeFileNames('1_a_xxx_yyy'));
> >
> > function changeFileNames(s) {
> >  short = substring(s,0,indexOf(s,'_',indexOf(s,'_')+1));
> >  paddedIndex = IJ.pad(parseInt(substring(short,0,indexOf(short,'_'))),3);
> >  return ''+paddedIndex+substring(short,indexOf(short,'_'));
> > }
> > ////
> >
> > Sincerely,
> > Jerome.
> >
> > On Sat, Apr 2, 2011 at 5:16 PM, Devrim Pesen Okvur <
> [hidden email]>
> > wrote:
> >>
> >> I would like to change tiff file names as follows:
> >> 1- place two zeros in the beginning so that the order is 001 002 ... 100
> > instead of 1 2 ...100
> >> 2- shorten the file name from 1_a_xxx_yyy to 1_a
> >>
> >> Combining the two above,  filename of 1_a_xxx_yyy will be changed to
> 001_a
> >>
> >> Any help is appreciated.
> >
>
>
>
> --
> Devrim Pesen Okvur, Ph.D.
> Izmir Institute of Technology
> Department of Molecular Biology & Genetics
> Faculty of Science A Block Room 225
> Gulbahce Campus 35430
> Urla Izmir Turkey
> Phone: +90 232 750 7510
> Fax: +90 232 750 7509
>