Kim
Kim

Reputation: 15

Java - Renaming a file automatically after searching a directory

When I run this program, it finds the file that starts with yyyy and returns the files correctly; however, it skips my.File.exists() and goes to else "File Name Change Failed". I have been looking at this code for a long time..what noob mistake am I making?

//Searches the path for files

 String pathToScan =("C:\\Users\\desktop\\test\\");
        String target_file ; 
        File folderToScan = new File(pathToScan); 

    File[] listOfFiles = folderToScan.listFiles();

     for (int i = 0; i < listOfFiles.length; i++) {
            if (listOfFiles[i].isFile()) {
                target_file =  listOfFiles[i].getName();
                if (target_file.startsWith("YYYY")){
                    System.out.println("-----------------------------------------------");
                    System.out.println("Match Found: "+ target_file); 
                //if the target file exists 
                if(myFile.exists()){

                    long lastmod = myFile.lastModified();
                    SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-DD");
                    String lastmodi =  format.format(new Date(lastmod));

                    File newfile = new File("C:\\Users\\desktop\\test\\"+lastmodi+".csv");

                    //If rename successful, then print success with file location
                    if(myFile.renameTo(newfile)){
                        System.out.println("File Name Change Successful, New File Created:" + newfile.getPath());
                    }       
                    else{
                    System.out.println("File Name Change Failed");
                    }
                }

Upvotes: 0

Views: 111

Answers (1)

shazin
shazin

Reputation: 21883

I think you are referring a wrong file in myFile. The following code is working for me with added code. The other reasons can be that you don't have the Access to Write in C:\Users\desktop\test\ directory and/or that directory doesn't exist.

    String pathToScan = ("C:\\Users\\<User Name>\\desktop\\test\\");
    String target_file;
    File folderToScan = new File(pathToScan);
    File myFile = null; // Added this

    File[] listOfFiles = folderToScan.listFiles();

    for (int i = 0; i < listOfFiles.length; i++) {
        if (listOfFiles[i].isFile()) {
            target_file = listOfFiles[i].getName();
            myFile = listOfFiles[i]; // Added this
            if (target_file.startsWith("YYYY")) {
                System.out
                        .println("-----------------------------------------------");
                System.out.println("Match Found: " + target_file);
                // if the target file exists
                if (myFile.exists()) {

                    long lastmod = myFile.lastModified();
                    SimpleDateFormat format = new SimpleDateFormat(
                            "YYYY-MM-DD");
                    String lastmodi = format.format(new Date(lastmod));

                    File newfile = new File(
                            "C:\\Users\\<User Name>\\desktop\\test\\"
                                    + lastmodi + ".csv");

                    // If rename successful, then print success with file
                    // location
                    if (myFile.renameTo(newfile)) {
                        System.out
                                .println("File Name Change Successful, New File Created:"
                                        + newfile.getPath());
                    } else {
                        System.out.println("File Name Change Failed");
                    }
                }
            }
        }
    }

Upvotes: 1

Related Questions