Question

I am trying to create a Java app (with swing) which renames pdf files with a set naming convention. Currently the files are named:

1.pdf, 2.pdf...... 10.pdf, 11.pdf...... 20.pdf, 21.pdf etc.

So I decide to add a prefix (ABC_0) to the file name. The new file names should be:

ABC_01.pdf, ABC_02.pdf.... ABC_10.pdf, ABC_11.pdf...... ABC_20.pdf, ABC_21.pdf etc.

So far everything is working well. The only problem I am facing is that when the prefix gets added to a pdf file name with a number 10 and above, it's renamed as:

ABC_010.pdf, ABC_011.pdf...... ABC_020.pdf, ABC_021.pdf etc.

This is wrong. The 0 should only be added to the pdf file names with a number 1–9 in it.

Could you please help me?

This is the code that I need help with.

        {
            String dir= txt_src.getText();
            String addPrefix= "ABC_0";
            File dirFile,dirFile1;
            File oldfile, newfile;
            String newname;
            String filenames[];
            int i, count;
            dirFile = new File(dir);
            if (!dirFile.exists() || !dirFile.isDirectory()) 
            {
                message("File not exist or not a directory");
            }
            filenames = dirFile.list();
            for(i = count = 0; i < filenames.length; i++) 
            {
                if (filenames[i].equals(".")) continue; 
                if (filenames[i].equals("..")) continue;
                dirFile1 = new File(dir+"\\"+filenames[i]); 
                if (!dirFile1.isDirectory())
                {oldfile = new File(dirFile, filenames[i]); 
                    newname = addPrefix + filenames[i]; 
                    newfile = new File(dirFile, newname); 
                    message("Files Renamed Successfully");
                    if (oldfile.renameTo(newfile)) count++; 
                    else 
                    {
                        message("Unable to rename " + oldfile);
                    }
                }
            }
        }
Was it helpful?

Solution

You can make sure that your filenames are padded with zeros like this:

public String pad(String fileName, int len) {
  if (fileName.length() >= len) {
    return fileName;
  }
  String padded = "0000000" + fileName;  // Change the number of zeros to your needs
  return  padded.substring(padded.length() - len); 
}

Then you just have to prepend "ABC_" to the padded value:

String newName = "ABC_" + pad(oldNmame, 6); // produce 6 characters per String

produces results like:

10.pdf gets ABC_10.pdf
1.pdf gets ABC_01.pdf
a.pdf gets ABC_0a.pdf
100.pdf gets ABC_100.pdf
a.a gets ABC_000a.a

OTHER TIPS

remove 0 from addPrefix

String addPrefix= "ABC_0";

use this

String addPrefix= "ABC_";

updated this line

 newname = addPrefix+i + filenames[i]; 

I would just create 2 different String variables and let it choose. Check if the number is less than 10, if it is, have it use the string with the 0 ABC_0

If it's greater than 10, use the string without the 0 ABC_

Check my little program. It reads the number before the extension ".pdf". If the number has a length of 1, add a 0 before adding a prefix.

String pdfNames[] = new String[] { "2.pdf", "6.pdf", "19.pdf", "26.pdf" };
String newNames[] = new String[pdfNames.length];
String prefix = "ABC_";
for (int i = 0; i < pdfNames.length; i++) {
    String name = pdfNames[i].split(".pdf")[0];
    System.out.println(name);

    newNames[i] = prefix;

    if (name.length() == 1)
        newNames[i] += "0";

    newNames[i] += name;
    System.out.println(newNames[i]);
}

But think about the case you have with files named like 123, 1234 and so on. Then you have to add more than one 0.

EDIT

In your code

filenames = dirFile.list();
for(i = count = 0; i < filenames.length; i++) 
{
    ...

you first fill your String array filenames with a list of file names ;-)

In the for loop you can get the length of the filename by filenames[i].split(".pdf")[0].length().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top