1

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

Sridhar
  • 11,466
  • 5
  • 39
  • 43
  • It's not such file in the directory, so maybe really it is because of the OS I am with Windows 8 –  Sep 08 '14 at 10:49
  • 1
    Windows can be funny about renames that only modify the case. Try renaming them to something else completely (for example add an _ at the start) and then rename them back with the case changed. (Obviously you will need to cope with cases where there are already files called _x) – Tim B Sep 08 '14 at 10:51
  • Instead of renaming you can try to move these files. It worked for me. http://docs.oracle.com/javase/tutorial/essential/io/move.html – Łukasz Kosiak Sep 08 '14 at 10:52

3 Answers3

1

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

ortis
  • 2,203
  • 2
  • 15
  • 18
0

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);

        }
    }

}
ErstwhileIII
  • 4,829
  • 2
  • 23
  • 37
0

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));
    }
}
Community
  • 1
  • 1
Sridhar
  • 11,466
  • 5
  • 39
  • 43