Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Chapter 12.29 ( Rename files ) suppose you have a lot of files in a directo

ID: 3864361 • Letter: J

Question

Java Chapter 12.29 (Rename files) suppose you have a lot of files in a directory named Exercisei_j, where i and j are digits. Write a program that pads a 0 before j if j is a single digit. For example, a file named Exercise2_1 in a directory will be renamed to Exercise2_01. In Java, when you pass the symbol * from the command line, it refers to all files in the directory (see Supplement III.V). Use the following command to run your program. java Exercise12_29 * can you make a example directory with sample files.. Please type you answer. Thanks

I have this code. but I cannot seem to make it rename the files.

RenameFiles.java:

import java.io.File;

public class RenameFiles {

   public static void main(String[] args) {
       if(args.length != 0) {
       for(String name: args) {
       File f = new File(name);
if(f.isFile()){
String newName = getNewName(f.getName());
if(newName != null) {
       File newFile = new File(newName);
   f.renameTo(newFile);
}
}
       }
       }
   }

   private static String getNewName(String name) {
       String tokens[] = name.split("_");
      
       // if string is ending on _ without any digit at last
       if(name.charAt(name.length()-1) == '_') {
           return null;
       }
      
       // if string has got _ then, there will be atleast two parts
       if(tokens.length >= 2) {
           String fullName = "";
           int i=0;
           while(i < tokens.length-1) {
               fullName += tokens[i] + "_";
               i++;
           }
          
           // if last token is single digit
           if(tokens[tokens.length - 1].length()==1) {
               if(Character.isDigit(tokens[tokens.length - 1].charAt(0))) {
                   fullName += "0" + tokens[tokens.length-1];
                   return fullName;                  
               } else
                   return null;
           } else
               return null;
       }
       return null;
   }
}

I believe the code should be Program can be run as : java RenameFiles * as shown in the above image. It changes the files names as expected.

I put the RenameFiles.java & class in the same folder in windows and the abc_1, abc_2 & abc_3. I have not been able the rename the as described. can you tell me what I am doing wrong?

Explanation / Answer

/// INPUT INSTRUCTIONS :

//=> YOUR FILE SHULD HAVE SOME EXTENTION THEN ONLY THAT FILE WILL WE TREATED AS FILE

//=> example your input file name is apb_1.txt then result will we apb_01.txt // Currect input

//=> if yout input file name is apb_1 then this will not consider as file // Incurrect Input

//=> Please Comment for any funtional modificaton but please clear your input and output with example

//===============================================================================//

import java.io.File;

public class RenameFiles {

   public static void main(String[] args) {
       if(args.length != 0) {
           for(String name: args) {
               File f = new File(name);
               if(f.isFile()){
                   String nameWithoutExtn = stripExtension(f.getName());
                   String extn = getExtension(f.getName());
                   String newName = getNewName(nameWithoutExtn);
                   if(newName != null) {
                       newName +=extn;
                       File newFile = new File(newName);
                       f.renameTo(newFile);
                   }
               }
           }
       }
   }

   private static String getNewName(String name) {
     
       String tokens[] = name.split("_");
       // if string is ending on _ without any digit at last
       if(name.charAt(name.length()-1) == '_') {
           return null;
       }
    
       // if string has got _ then, there will be atleast two parts
       if(tokens.length >= 2) {
           String fullName = "";
           int i=0;
           while(i < tokens.length-1) {
               fullName += tokens[i] + "_";
               i++;
           }
           // if last token is single digit
           if(tokens[tokens.length - 1].length()==1) {
               if(Character.isDigit(tokens[tokens.length - 1].charAt(0))) {
                   fullName += "0" + tokens[tokens.length-1];
                   return fullName;                
               } else
                   return null;
           } else
               return null;
       }
       return null;
   }
   static String stripExtension (String str) {
       // Handle null case specially.
       if (str == null) return null;
       // Get position of last '.'.
       int pos = str.lastIndexOf(".");
       // If there wasn't any '.' just return the string as is.
       if (pos == -1) return str;
       // Otherwise return the string, up to the dot.
       return str.substring(0, pos);
   }
   static String getExtension (String str) {
       if (str == null) return null;
       int pos = str.lastIndexOf(".");
       if (pos == -1) return str;
       return str.substring(pos, str.length());
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote