0

I currently have a big folder full of filenames in the format:

EXA_0100_01012014.csv
EXA_0114_11012014.csv

Always the same 3 letters at the start. I need to change all of these filenames so that they are in the format:

EXA_B_0100_01012014

So it's just a case of inserting an _B (always _B) after the first three letters. I'm only just started learning Java so my attempts so far are fairly limited:

File oldfile = new File("EXA_0100_01012014.csv");
File newfile = new File("EXA_B_0100_01012014.csv");

I just need to do this for a large number of files all with the same 3 letter prefix. All the numbers change from file to file though.

If someone could give me a nudge in the right direction it would be much appreciated.

Thanks.

Tom
  • 15,798
  • 4
  • 37
  • 48

5 Answers5

2

Use substring.

String fileName = "EXA_0100_01012014";
String newFileName = fileName.substring(0, 3) + "_B_" + fileName.substring(4);

Returns newFileName as:

EXA_B_0100_01012014
Tom
  • 15,798
  • 4
  • 37
  • 48
1

My suggestion:

String newFilename = oldfile.getFileName().replace("EXA_", "EXA_B_");
oldfile.renameTo(new File(newFilename));

If you don't like the replace() approach you could use the substring() method instead.

String oldFilename = oldfile.getFileName();
String newFilename = oldFilename.substring(0, 3) + "_B_" + oldFilename.substring(4);
oldfile.renameTo(new File(newFilename));
Bruno Toffolo
  • 1,504
  • 19
  • 24
0

Here is the results from a quick google bomb: First start looking at the renaming a file, Then you can instert a string by breaking the substrings apart and the prepending the first 3 characters and appending the rest after "_B". Similar to this.

Community
  • 1
  • 1
maximx1
  • 89
  • 5
0
public static void main(String[] h) {

    final File folder = new File("/home/you/Desktop");
    renameFilesForFolder(folder);

}

public static void renameFilesForFolder(final File folder) {
    for (final File fileEntry : folder.listFiles()) {
        if (fileEntry.isDirectory()) {
            renameFilesForFolder(fileEntry);
        } else {
            if (fileEntry.getName().startsWith("EXA")) {
                fileEntry.renameTo(new File(fileEntry.getName().replaceAll("(EXA)(_)", "$1_B$2")));
            }
        }
    }
}
Melih Altıntaş
  • 2,495
  • 1
  • 22
  • 35
0

Here is a possible solution.

I used the following links to help me:

  1. Nagesh Chauhan's solution for file renaming http://www.beingjavaguys.com/2013/08/create-delete-rename-file-in-java.html

  2. Philip Reichart's solution on file list How to get contents of a folder and put into an ArrayList

    import java.io.File; import java.io.IOException;

    public class RenameFiles {

    public RenameFiles()
    {
        File f = new File ("C:/work/play/java/list");
        File[] list = f.listFiles();
    
        for (int inum = 0; inum < list.length; inum++)
        {
            File curf = list[inum];
            RenameFile(curf);
        }
    }
    
    public void RenameFile(File curf) 
    {
        String strfilename = curf.getAbsolutePath();
        StringBuffer buf = new StringBuffer(strfilename);
        int index = buf.indexOf("EXA_");
        buf.insert(index+4, "B_");
    
        String strnewfilename = buf.toString();
        strnewfilename = strnewfilename.replace('\\', '/');
        System.out.println(strnewfilename);
    
        File newFileName = new File(strnewfilename);
    
        try {  
            if (curf.renameTo(newFileName)) {  
                System.out.println("File "+strfilename+"renamed to "+strnewfilename+" successful !");  
            } else {  
                System.out.println("File "+strfilename+"renamed to "+strnewfilename+" failed !");  
            }  
    
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
     }
    
    public static void main(String[] args)
    {
        RenameFiles fobj = new RenameFiles();               
    }
    

    }

Community
  • 1
  • 1
Rom El
  • 9
  • 1