Thursday, July 11, 2013

Copy Files

FileCopy is a simple program that can help you to copy files in your local computer from a source directory to a destination directory. There are two choices for you. You can copy files by extension. If you want to copy all files, write *. for the file extension.

copy files by extensions


Another option is that you can copy files by date modified. If you want copy files that their date modified is equal to your date input, write 0 for comparison parameter. By writing -1 for the comparison parameter, files with dates modified greater than your date input are copied. In contrast, writing 1 for the comparison parameter is to copy files that have dates modifies less than the date input.

copy files by date modified

FileCopy source code:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.Date;
import org.joda.time.LocalDate;

public class FileCopy{
public static void main(String[] args){

                System.out.println("........EASY FILES COPY......");
startProgram();

}

public static void copyFileByExt(){
try{
//create text reader to read text from keyboard
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//allow source directory input
System.out.println("Enter source directory:");
String from_dir=br.readLine();
//create file object to encapsulate the source directory
File fromDir=new File(from_dir);
//check source directory existence
if(!fromDir.exists())
{
System.out.println("Source directory deos not exist.");
System.exit(-1);
}
//allow destination directory input
System.out.println("Enter destination directory:");
String to_dir=br.readLine();
//create file object to encapsulate the source directory
File toDir=new File(to_dir);
if(!toDir.exists())
toDir.mkdir();
//allow file extension input
System.out.println("Enter file extension:");
String ext=br.readLine();
if(ext!=""){
String[] files;
if(ext.equals("*."))
files=fromDir.list(); //select all files
else
files=fromDir.list(new ExtFilter(ext)); //select only files of specified extension
System.out.println("Please wait...");
for(String s:files){
File myfile=new File(fromDir+"/"+s);
if(myfile.isFile()){
System.out.println("Copying "+fromDir+"/"+s+" to "+to_dir);
copy(fromDir+"/"+s,toDir+"/"+s);
}
}
System.out.println("Compy completed.");

}
}catch(IOException ie){ie.printStackTrace();}
}

public static void copyFileByModifyDate(){
try{
//create text reader to read text from keyboard
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//allow source directory input
System.out.println("Enter source directory:");
String from_dir=br.readLine();
//create file object to encapsulate the source directory
File fromDir=new File(from_dir);
//checking source directory existence
if(!fromDir.exists())
{
System.out.println("Source directory deos not exist.");
System.exit(-1);
}
//allow destination directory input
System.out.println("Enter destination directory:");
String to_dir=br.readLine();
//create file object to encapsulate the destination directory
File toDir=new File(to_dir);
if(!toDir.exists())
toDir.mkdir();
//allow the date modified input used for comparison
System.out.println("Enter the date modified(e.g. 2013-02-16):");
String modify_date=br.readLine();
//allow comparison value input
//0 means copy all files that their dates modified  are equal to the input date
//1 means copy all files that their dates modified  are less than the input date
//-1 means copy all files that their dates modified  are greater than the input date

System.out.println("Enter comparison value (0, 1, or -1):");
int com=Integer.parseInt(br.readLine());
String[] files=fromDir.list();
System.out.println("Please wait...");
for(String s:files){
File myFile=new File(from_dir+"/"+s);
if(myFile.isFile()){
LocalDate localdate=LocalDate.parse(modify_date);
LocalDate modate=LocalDate.fromDateFields(new Date(myFile.lastModified()));
int x=localdate.compareTo(modate);
if(x==com){
System.out.println("Copying "+from_dir+"/"+s+" to "+to_dir);
copy(from_dir+"/"+s,to_dir+"/"+s);
}
}
}
System.out.println("Copy completed");

}catch(IOException ie){}

}
public static void copy(String srcfile, String descfile){
try{
//create stream reader
FileInputStream fis=new FileInputStream(srcfile);
//create stream writer
FileOutputStream fos=new FileOutputStream(descfile);
int data;
//start copying
while((data=fis.read())!=-1){
fos.write(data);

}
fos.flush();
fos.close();
fis.close();
}catch(IOException ie){}

}

public static void startProgram(){
int ch;
char con='y';
Scanner sc=new Scanner(System.in); //create scanner object to receive choice input

while(con=='y'){
showCopyOptions(); //show menu
System.out.println("Enter your choice:");
ch=sc.nextInt();
switch(ch){
case 1:copyFileByExt();break;
case 2:copyFileByModifyDate();break;
case 3:System.exit(0);break;
default:System.out.println("Invalid choice");
}

try{
//prompt for continuing the program
InputStreamReader isr=new InputStreamReader(System.in);
System.out.println("Press y to continue:");
con=(char)isr.read();
}catch(IOException ie){}


}
}
public static void showCopyOptions(){
System.out.println("1. Copy files by extension");
                System.out.println("2. Copy files by date modified");
System.out.println("3. Exit program");
}

}

class ExtFilter implements FilenameFilter{
String ext="";
public ExtFilter(String ext){
this.ext="."+ext;
}
public boolean accept(File f, String name){
if(name.endsWith(ext)) return true;
else return false;

}
}


1 comment:

  1. FileCopy is a simple program that can help you to copy files in your local computer from a source directory to a destination directory. There are two choices for you. You can copy files by extension. If you want to copy all files, write *. for the file extension. pakistani designer lawn suits 2016 , lawn suit stitching designs 2016

    ReplyDelete