How to rename bunch of files with numbers (with leading zeros)?
I mentioned in my earlier post Day-today Programming 2 about the simple java program to rename files with numbers.
eg:-
1.MP3
2.MP3
3.MP3 ...
As I said there were about 200 MP3s. Mom wanted them to play in the numbered order. But when the DVD player plays them after playing 1.MP3 it didn't play 2.MP3 next. It played 10.MP3 instead. That was because it plays them in ascending order.
When it plays in ascending order, after 1, the next one is 10, and the next one is 100. It plays 2.Mp3 after finishing all the tracks that start from digit 1.
So I had to write the DVD again. This time I have to rename them in a different way.
If I rename them with leading zeros, it will play them in the order as numbered.
eg:-
001.MP3
002.MP3
...
010.MP3
...
100.MP3
In order to do that, I changed my previous renaming program a little bit.
This might not be the most efficient way of doing that. But for time being, it was good enough.
In this case, the length of all the file names should be constant.
The number of '0's for the first file which is 1, is determined by iterating from 1 to the length of the longest number.
(If there are 200 files the longest number will have 3 digits. And the first file will be 001)
Then the number of leading zeros should be decremented whenever the iterator is a power of 10. To check that I took the logarithm of the iterator and checked if it is an integer. And also have to skip the logarithm of 1.
Finally, I could write the DVD and it played as mom expected. Everybody is happy :) :)
I mentioned in my earlier post Day-today Programming 2 about the simple java program to rename files with numbers.
eg:-
1.MP3
2.MP3
3.MP3 ...
As I said there were about 200 MP3s. Mom wanted them to play in the numbered order. But when the DVD player plays them after playing 1.MP3 it didn't play 2.MP3 next. It played 10.MP3 instead. That was because it plays them in ascending order.
When it plays in ascending order, after 1, the next one is 10, and the next one is 100. It plays 2.Mp3 after finishing all the tracks that start from digit 1.
So I had to write the DVD again. This time I have to rename them in a different way.
If I rename them with leading zeros, it will play them in the order as numbered.
eg:-
001.MP3
002.MP3
...
010.MP3
...
100.MP3
In order to do that, I changed my previous renaming program a little bit.
This might not be the most efficient way of doing that. But for time being, it was good enough.
//author: Thilina
//Program to rename files with numbers including leading zeros
package org.filenamestotxt;
import java.io.File;
public class Rename2Numbers {
public static void main(String[] args){
File folder = new File("E:/abc");
//file extension
String ext= ".mp3";
// iterator
int i=1;
//number of digits in for the filename
int l = (folder.list().length+"").length();
String newName= "";
String zeros="";
//determine how many leading zeros
for (int j = 1; j < l; j++) {
zeros = zeros + "0";
}
//variable to store logarithm
double logarithm=0;
for(File f : folder.listFiles()){
logarithm = Math.log10(i);
//check if log10(i) is an integer
if(i!=1 & (logarithm==(int)logarithm))
//if true remove one zero from leading zeros
zeros = zeros.substring(1);
newName = zeros+i+ext;
f.renameTo(new File("C:/My Folder/"+newName));
i++;
}
}
}
In this case, the length of all the file names should be constant.
The number of '0's for the first file which is 1, is determined by iterating from 1 to the length of the longest number.
(If there are 200 files the longest number will have 3 digits. And the first file will be 001)
//determine how many leading zeros
for (int j = 1; j < l; j++) {
zeros = zeros + "0";
}
Then the number of leading zeros should be decremented whenever the iterator is a power of 10. To check that I took the logarithm of the iterator and checked if it is an integer. And also have to skip the logarithm of 1.
logarithm = Math.log10(i);
if(i!=1 & (logarithm==(int)logarithm))
zeros = zeros.substring(1);
Finally, I could write the DVD and it played as mom expected. Everybody is happy :) :)
Comments
Post a Comment