Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Copy folder based on latest date

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Copy folder based on latest date

    Hi,

    Can anybody send me the java code to do following:

    - A directory containing sub-folders with different name and date
    - To copy the folder having the most recent date from the directory.

    Thanks in advance.

    Ayon


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Copy folder based on latest date

    Please show us your thoughts/code attempts to solve the problem and we will help you

    You will probably want to use the File Class.

  3. #3
    Junior Member
    Join Date
    May 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Copy folder based on latest date

    Well , I tried using File class, which has a method called file.lastmodified(), this returns long value of the time.
    Using this I am not able to sort the latest folder created. Can you please suggest me , how to sort based on long value?

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Copy folder based on latest date

    The long value returned is the number of milliseconds from a pre-determined time (I believe it's Jan. 1, 1960, but don't quote me on this, and it doesn't really matter for sorting). So, the most recently modified file is the file that has the larges long value returned, and the oldest has the smallest value.

    If you need to convert this long value to a Date object, I think there's a method inside the Date class, or possibly one of the Calendar classes (the Java Date/Time representation is a little lacking here)

  5. #5
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Copy folder based on latest date

    Quote Originally Posted by helloworld922 View Post
    If you need to convert this long value to a Date object, I think there's a method inside the Date class...
    There's both a constructor and a setTime(..) method that take a long as milliseconds.

  6. #6
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Copy folder based on latest date

    Basically use the File class and create an instance which points to your base directory.

    Use file.listFiles() get all the files in the base directory.
    Use a comparator to sort the array by lastModified.
    Grab the newest file and add it to your list.
    Get all directories in the base directory and do the same for each directory.

    As a reply to your question on sorting this is what you could do.

            Arrays.sort(files, new Comparator<File>() {
                @Override
                public int compare(File o1, File o2) {
                    return (int) (o2.lastModified() - o1.lastModified());
                }
            });

    Assuming files is an array of File objects. This would put the last edited file/directory at index zero.

    // Json

  7. #7
    Member
    Join Date
    May 2010
    Posts
    39
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default Re: Copy folder based on latest date

    Somthing like?

    	private static File getRecent(String directory) {
    		File file = null;
    		for(String x : new File(directory).list()) {
    			File inQuestion = new File(directory + x);
    			if(file == null) {
    				file = inQuestion;
    				continue;
    			}
    			if(inQuestion.lastModified() < file.lastModified()) {
    				file = inQuestion;
    			}
    		}
    		return file;
    	}

    It returns the most recently created file in the directory,

  8. #8
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Copy folder based on latest date

    Hello Time, would that not also return you a directory if the latest modified file in the directory was actually another directory?

    // Json

  9. #9
    Member
    Join Date
    May 2010
    Posts
    39
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default Re: Copy folder based on latest date

    Quote Originally Posted by Json View Post
    Hello Time, would that not also return you a directory if the latest modified file in the directory was actually another directory?

    // Json
    Just do, if(!inQuestion.isDirectory())

  10. #10
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Copy folder based on latest date

    or if(inQuestion.isFile()) perhaps

    // Json

  11. #11
    Junior Member
    Join Date
    May 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Copy folder based on latest date

    Thanks , I have tried this and somehow gotthis working.

    Thanks again for your help

Similar Threads

  1. Loading a file in a different folder
    By tiemykim in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 15th, 2010, 09:53 AM
  2. Getting information from a folder
    By shadihrr in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 23rd, 2010, 04:13 PM
  3. Icon change and lib folder problem
    By LeonLanford in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 21st, 2009, 04:00 AM
  4. [SOLVED] installation procedure for java 1.6 latest version on xp
    By sriraj.kundan in forum Java Theory & Questions
    Replies: 1
    Last Post: June 25th, 2009, 09:18 AM
  5. Java program to take new files from one path and sent it do another path?
    By jazz2k8 in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: July 8th, 2008, 07:47 AM