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

Install Docker on Windows 11 with WSL Ubuntu 22.04

This is to install Docker within Ubuntu WSL without using the Windows Docker application. Follow the below steps. Install Ubuntu 22.04 WSL 1. Enable Windows Subsystem for Linux and Virtual Machine platform Go to Control Panel -> Programs -> Programs and Features -> Turn Windows features on or off 2. Switch to WSL 2 Open Powershell and type in the below command. wsl --set-default-version 2 If you don't have WSL 2, download the latest WSL 2 package and install it.  3. Install Ubuntu Open Microsoft Store and search for Ubuntu. Select the version you intend to install. I'd use the latest LTS version Ubuntu 22.04. Click on the Get button. It will take a couple of minutes to download and install. 4. Open up the installed Ubuntu version that was installed. If you get an error like the below image, make sure to install the WSL2 Kernel update .  If it's an older Ubuntu version the error message would be something like the image below. Error: WSL 2 requires an update to its ...

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

Automatically open Chrome developer tools in a new tab

Sometimes we need to check the console or the network transactions of a link that opens up in a new tab. By default, the Chrome developer tools are not opening in a new tab. So, by the time when we hit F12 and open the dev tools, part of the information we needed could be already gone.  There's a setting in dev tools where you can keep the dev tools open automatically in a new tab. To enable that, hit F12 and open up the dev tools. Click on the settings icon in the top right corner. In the Preferences section, scroll down to the bottom. You'll be able to find the option to Auto-open DevTools for popups. Select the checkbox and we're good to go!