user3662019
user3662019

Reputation:

Java renaming files in directory doesn't work properly

I am trying to rename to upper case all the files in a given directory. It does the whole thing but it doesn't do anything in the folder file names are still the same .

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

public class FileOps {

    public static void main(String[] argv) throws IOException {

        File folder = new File(
                "C:\\Users\\N\\Desktop\\New folder\\RenamingFiles\\src\\renaming\\Files");
        File[] listOfFiles = folder.listFiles();

        for (int i = 0; i < listOfFiles.length; i++) {
            if (listOfFiles[i].isFile()) {
                File f = new File(
                    "C:\\Users\\N\\Desktop\\New folder\\RenamingFiles\\src\\renaming\\Files"
                            + listOfFiles[i].getName());
                f.renameTo(new File(
                    "C:\\Users\\N\\Desktop\\New folder\\RenamingFiles\\src\\renaming\\Files"
                            + listOfFiles[i].getName().toUpperCase()
                            + ".txt"));
            }
        }

        System.out.println("Done");
    }
}

It prints "Done" in the console but nothing is really done

Upvotes: 1

Views: 1781

Answers (3)

Sridhar
Sridhar

Reputation: 11786

As pointed by ortis, you have missed to add "\" while naming files.

                f.renameTo(new File(
                "C:\\Users\\N\\Desktop\\New folder\\RenamingFiles\\src\\renaming\\Files"
                        + listOfFiles[i].getName().toUpperCase()
                        + ".txt"));

executing this code over and over will result in adding .txt to the file names. You can consider using apache FileUtils for getting extention.

Making those changes to your code,

File folder = new File("/home/ubuntu/Desktop/pics");
File[] listOfFiles = folder.listFiles();

for(File file : listOfFiles){
    if(file.isFile()){          
        String fileName =   FilenameUtils.removeExtension(file.getName()).toUpperCase() ; 
        String newPath = folder + File.separator + fileName+ "."+ FilenameUtils.getExtension(file.getName());
        file.renameTo(new File(newPath));
    }
}

Upvotes: 0

ErstwhileIII
ErstwhileIII

Reputation: 4843

You might use the following to check what is happening. Some small changes include using the File(parent,name) form to avoid having to determine and add the OS specific file path separator.

package com.example.renaming;

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

public class TestRename {
    private static final String[] defaultArgs = { "C:\\Users\\N\\Desktop\\New folder\\RenamingFiles\\src\\renaming\\Files" };
    private static TestRename me;
    private static String[] arguments;

    public static void main(String[] args) {
        me = new TestRename();
        if (args == null | args.length == 0) {
            arguments = defaultArgs;
        } else {
            arguments = args;
        }
        me.doWork(arguments);

    }

    private void doWork(String[] arguments) {
        int numFiles = 0;
        File folder = new File(arguments[0]);

        try {
            System.out.println("Working on " + folder.getCanonicalPath());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        File[] fileList = folder.listFiles();
        if (fileList == null) {
            System.out.println("No files fould");
        } else {

            for (int i = 0; i < fileList.length; i++) {
                System.out.println("File " + fileList[i].getName());
                if (fileList[i].isFile()) {
                    numFiles++;
                    try {
                        String currentName = fileList[i].getName();
                        File parent = fileList[i].getParentFile();
                        String newName = currentName.toUpperCase() + ".txt";
                        System.out.println(" .. current = " + currentName);
                        System.out.println(" .. newname = " + newName);

                        // Avoids having to get the file path separator for an OS
                        File newFile = new File(parent, newName);
                        System.out.println(" .. new File = "
                                + newFile.getCanonicalPath());
                        fileList[i].renameTo(newFile);

                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }

            System.out.println("Done, found " + numFiles);

        }
    }

}

Upvotes: 0

ortis
ortis

Reputation: 2223

In your if statement, you forgot to add ending separator:

 if (listOfFiles[i].isFile()) {
        File f = new File(
                "C:\\Users\\N\\Desktop\\New folder\\RenamingFiles\\src\\renaming\\Files\\"// <- Missing separator
                        + listOfFiles[i].getName());
        f.renameTo(new File(
                "C:\\Users\\N\\Desktop\\New folder\\RenamingFiles\\src\\renaming\\Files\\"// <- Missing separator
                        + listOfFiles[i].getName().toUpperCase()
                        + ".txt"));
 }

A proper implemntation would be:

if (listOfFiles[i].isFile()) 
         listOfFiles[i].renameTo(new File(folder, listOfFiles[i].getName().toUpperCase()+ ".txt"));//not sure why this .txt

Be careful, the renameTo method is highly platform dependent. Read the Javadoc

Upvotes: 1

Related Questions