Skip to main content

Day-to-day Programming 3: Rename Files with numbers - part 2

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.


 //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

Popular posts from this blog

Java, how to create a list with a single element

 I wanted to create a Java List with a single element. Yet, I wanted to add more elements later. So, I was looking for a couple of ways to do this. So far there are multiple elegant ways to create a list with a single element with a one-liner. Not so much for a modifiable list though. Here's what I gathered so far. Followings are a few ways of creating a list with strictly a single entry. Can't add more elements. 1. Collections.singletonList() This returns an immutable list that cannot be modified or add more elements. // An immutable list containing only the specified object. List<String> oneEntryList = Collections. singletonList ( "one" ) ; oneEntryList.add( "two" ) ; // throws UnsupportedOperationException 2.  Arrays.asList() This returns a fixed-size list consisting of the number of elements provided as arguments. The number of elements provided would be an array hence the size is fixed to the length of the array. // Returns a fixed-size list List...

How to fix SSLHandshakeException PKIX path building failed in Java

TL ; DR 1. Extract the public certificate of the website/API that you are trying to connect from your Java application. Steps are mentioned in this post 2. Use the Java keytool to install the extracted certificate into the "cacerts" file (Trust store) keytool -import -trustcacerts -alias <domain name> -file <public certificate>.cert -keystore /path_to_java_home/jre/lib/security/cacerts -storepass changeit 3. Restart your Java application Exception A typical exception stack trace would look like below. javax.net.ssl. SSLHandshakeException : sun.security.validator.ValidatorException: PKIX path building failed : sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at sun.security.ssl.Alerts.getSSLException(Alerts.java:192) at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1959) at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302) at sun.security.ssl.Handshake...

Ubuntu DNS issue fix DNS_PROBE_FINISHED_BAD_CONFIG

Issue  I've been playing with a VPN and somehow it messed up my DNS resolution configurations. Chrome gives  DNS_PROBE_FINISHED_BAD_CONFIG  error and can't ping google. So it seemed to be an issue with the DNS. Of course, restarting didn't fix it. I tried DNS lookup which gave me below. To make sure this is somehting to do with my DNS confgis, I ran the same by providing the google DNS servers.  It works, which means my default DNS is not working for some reason. To make sure this, ran the below command. systemd-resolve --status Output has an entry for DNS Servers, which was  ::1 Fix 1. Edit the file /etc/systemd/resolved.conf. sudo vi /etc/systemd/resolved.conf 2. Add new DNS entries. I added 2 google DNS and the cloudflare DNS sever. [Resolve] DNS=8.8.8.8 8.8.4.4 1.1.1.1 3. Restart the systemd-resolved and check the configuration is persisted in /run/systemd/resolve/resolv.conf file. sudo service systemd-resolved restart cat /run/systemd/resolve/resolv.co...