1

I have 2 folder on different network drives Q: and K:

Daily i am passing a folder name with or without amendment number, the program searches for the file in Q: drive and if amendment no is not there, it copies the whole folder to K: drive's folder and if amendment no is there, it searches for that amendment folder inside the main folder and copies only that folder to K: drive's folder.

Right now I am copying without comparing file size or last modified date files in both the folders. In case of amendments, if file size is not checked, the code copies all of the files in the folder, which is waste of space on the disk (since there are already around 20000 folders, 100 GB size (of the folder).

How do i check for file size or last modified date and if any one is changed, then copy the folder?

For example, if i want to copy a folder by name 123456 without amendment, the code checks if the folder already exists on destination (does not check file size, etc) and then asks if you want to overwrite it or not and then copies the files.

if the folder name is 123456 and amendment no is 2 then the code, searches for the folder 123456 in source first and then amendment 2 folder (folder name itself is amendment 2) and then copies (if exists on destination, asks to overwrite) and then copies. Some of the files in the folders amendment 0, amendment 1 and amendment 2 remain the same. Only few of files would have changed.

I don't want the common files to be copied always, they have to be in root folder only and only the files changed have to be copied in amendment folders.

I am using only java for this. Here is my code

public static void copyFolder(File src, File dest, File destf) throws IOException
    {
             if(src.isDirectory())
            {
                     //if directory does not exist, create it
                     if(!dest.exists())
                    {
                               dest.mkdir();
                               System.out.println("Directory copied from " + src + "  to " + dest);
                    }

                     //list all the directory contents
                     String files[] = src.list();

                     for (String file : files)
                    {
                            //construct the src and dest file structure
                               File srcFile = new File(src, file);

                               File destFile = new File(dest, file);

                               File dst = new File(destf, file);

                            if(((dst.lastModified() - srcFile.lastModified()) == 0) || ((dst.length() - srcFile.length()) == 0))
                            {
                                    int op = JOptionPane.showConfirmDialog(null, srcFile.getName() + " File exists! \n Do you want to overwrite it?", "Confirm Window", JOptionPane.YES_NO_OPTION);

                                     if (op == JOptionPane.NO_OPTION)
                                            JOptionPane.showMessageDialog(null, "Copy Canceled");

                                    else
                                            copyFolder(srcFile, destFile, dst);
                            }

                            else
                                    copyFolder(srcFile, destFile, dst);
                     }
             }

            else
            {
                    if(((dest.lastModified() - src.lastModified()) == 0) || ((dest.length() - src.length()) == 0))
                    {
                            int op = JOptionPane.showConfirmDialog(null, src.getName() + " File exists! \n Do you want to overwrite it?", "Confirm Window", JOptionPane.YES_NO_OPTION);

                             if (op == JOptionPane.NO_OPTION)
                                    JOptionPane.showMessageDialog(null, "Copy Canceled");

                            else
                            {
                                    InputStream in = new FileInputStream(src);
                               OutputStream out = new FileOutputStream(dest);

                               byte[] buffer = new byte[1024];

                               int length;

                                    while ((length = in.read(buffer)) > 0)
                                    {
                                                 out.write(buffer, 0, length);
                                       }

                               in.close();
                               out.close();

                               System.out.println("File copied from " + src + " to " + dest);
                            }
                    }

                    else
                    {
                            InputStream in = new FileInputStream(src);
                       OutputStream out = new FileOutputStream(dest);

                       byte[] buffer = new byte[1024];

                       int length;

                            while ((length = in.read(buffer)) > 0)
                            {
                                         out.write(buffer, 0, length);
                               }

                       in.close();
                       out.close();

                       System.out.println("File copied from " + src + " to " + dest);
                    }
             }
     }
user1416631
  • 195
  • 1
  • 4
  • 15
  • You want to check if the file has been last modified since...when? And by 'amendment no' do you mean amendment number or no amendment has been made? – Mdev Apr 09 '14 at 04:22
  • 1
    Hi, thanks for the reply. It is amendment number – user1416631 Apr 09 '14 at 04:28

2 Answers2

1

How do i check for file size

File#length()

or last modified date

File#lastModified()

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

Some examples of different methods to copy a file including performance times: http://examples.javacodegeeks.com/core-java/io/file/4-ways-to-copy-file-in-java/

A stackoverflow answer related to this topic: Standard concise way to copy a file in Java?

But you can use file1.lastModified() != file2.lastModified() as a simple date check or .length as a length check.

File f1 = new File("c:\test\aaa\temp.txt"); 
File f2 = new File("d:\test\aaa\temp.txt");

// Same modify date?
if ((f1.lastModified() != f2.lastModified()) || (f1.length() != f2.length())) {
    // add to copy list
}

You need to be careful of the last modified date because you aren't copying the exactly as is such as the linux cp -a command. Rather, you're copying the file as you normally would, which is creating a new file in it's place and streaming the content into the file. In this case, .length() would be sufficient. You can always generate a CRC32 or MD5 hash of a file to check to see if the contents match. However it's a slow process if you're copying very large or many thousands of files.

Edit

Because last modified date is an important check for you, and you need to retain the last modified date attribute about a copied file, you can manually set it by:

File f1 = new File("c:\test\aaa\temp.txt"); 
File f2 = new File("d:\test\aaa\temp.txt");

if (!f1.exists()) {
    // throw an exception
}

// Copy the file as is, retain attributes, replace existing
CopyOption[] options = new CopyOption[] {COPY_ATTRIBUTES, REPLACE_EXISTING};
if (f2.exists()) {
    if ((f1.lastModified() != f2.lastModified()) || (f1.length() != f2.length()) {
        Files.copy(f1, f2, options);
    }
} else {
    Files.copy(f1, f2, options);
}

This relies on Java SE 7.

Here's an example from the Java site for copying files:

http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/essential/io/examples/Copy.java

You should also look at:

http://docs.oracle.com/javase/7/docs/api/java/io/File.html

and

http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html

Community
  • 1
  • 1
Justin Mitchell
  • 339
  • 1
  • 5
  • Hi, thanks for the reply, let me check and revert – user1416631 Apr 09 '14 at 04:29
  • I would just create two test files on your desktop first and check it that way before trying to apply it to the document tree. – Justin Mitchell Apr 09 '14 at 04:35
  • Sorry, I din't get your point. Could you brief on it? – user1416631 Apr 09 '14 at 04:53
  • Create a file on your hard drive, copy it, edit it, and then run the `.lastModified()` and `.length()` checks `f1 != f2` before you apply it to your full file copy operationg. – Justin Mitchell Apr 09 '14 at 04:55
  • ok, lemme give it a try – user1416631 Apr 09 '14 at 04:56
  • hi, i tried, but, i have the following difficulties.. What if i have 3 files in source and 2 files in destination? what if one of them is same or both of them are same? same means file.length() for files and file.lastModified() for files is same. how to check if the file exists, but not the latest version is there in destination, or if file does not exist, how to copy or check? – user1416631 Apr 09 '14 at 08:14
  • i could to some extent solve, but, i need to copy c:\test\aaa\temp.txt to d:\test\aaa\temp.txt, first i have to check if same temp.txt (file size, last modified date is all same,) is on d:\test folder. if same, then should not copy else, copy to d:\test\aaa\ folder (i.e., first, check on root folder test, if teh file is same, the no copy else copy temp.txt to d:\test\aaa folder). how to do this? – user1416631 Apr 09 '14 at 10:25
  • Updated answer hopefully that should help – Justin Mitchell Apr 09 '14 at 11:54
  • hi, thanks for the reply. i have written the same code. but, as i have written in the above comment i have to check is the same file(content and date same) in the root folder d:\test. if it exists, then copy cancelled else need to copy the folders or files. i have achieved it for files. but, i am struck for folders. as i have mentioned an example in my previous comment. for example, if i have to copy a – user1416631 Apr 10 '14 at 05:00
  • if it exists, then copy cancelled else need to copy the folders or files. i have achieved it for files. but, i am struck for folders. as i have mentioned an example in my previous comment. for example, if i have to copy a folder aaa in which there is a file temp.txt and and a folder xyz inside aaa folder. the same file temp.txt is in the source folder. so temp.txt should not be copied. only xyz folder has to be copied. i am able to manage temp.txt copy to be cancelled (displays message to user saying teh file exists) but, even the folder xys is not copied. please guide me – user1416631 Apr 10 '14 at 05:15
  • i have edited my post that shows the latest code i am using. in the above code, destf or dst is the folder that has the path to the root folder – user1416631 Apr 10 '14 at 05:22
  • hi, what if the modified date is different but the file size and contents are the same? what to do in this case? – user1416631 Apr 10 '14 at 11:17
  • As mentioned before, unless you're doing a file and maintaining the attributes, such as last edited by, last modified date etc. (the metadata contained on the file system about a file), then the last modified dates will be different. You can however, **manually** set the last modified date with `File.setLastModified`. Check the updated answer. – Justin Mitchell Apr 10 '14 at 23:55
  • hi, thanks for the reply. i have used file.setLastModified() in my latest code (have not updated it here). but, let me try out the copyoptions that you have mentioned. – user1416631 Apr 11 '14 at 02:50
  • hey change the code little bit. working now...... thanks – user1416631 Apr 14 '14 at 06:09
  • Sure, feel free to modify the post or post what changes you did her so that I can update the post above. Thanks :) – Justin Mitchell Apr 14 '14 at 23:48
  • it is internal to my work, but, thanks as the main logic is all yours......... – user1416631 Apr 15 '14 at 03:26