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