-2

I am trying to write a java program which will move some xml files form one folder to another based on its created/modified date.

For example, input will be (in DD-MM-YYYY:HH:mm:ss)

start date : 21-09-2016:00:00:00
End date : 21-09-2016:11:00:00

Can you please provide an example for the same?

Thanks in advance

1 Answers1

0

I assume that you would have your XML files within a directory (possibly a directory hierarchy too). You basically need to do the following :

  1. Traverse the directory using depth-first search to get all XML files under any level in the directory. You can find how to do this here
  2. For each java.nio.file.Path you can then get the BasicFileAttributes. The BasicFileAttributes class contains all required information about the basic file attributes such as created dates, modified dates etc. The below code snippet should help you get the BasicFileAttributes for a path

    Path file; //initialize this file variable to path you obtained in the DFS logic above

    BasicFileAttributes attrs = Files.readAttributes(file, BasicFileAttributes.class);

  3. You can now fetch the created/modified time from the attrs variable and compare it against the dates specified. It would be recommended to convert the dates input to a millisecond representation at the beginning of the program so that you can perform the comparison more naturally. Based on the results of the comparison you can then move the XML file to another location.

Make sure that you are wrapping up the above operation within a try-with-finally block and catching IOException wherever required.

Hope this helps.

Community
  • 1
  • 1
Prahalad Deshpande
  • 4,709
  • 1
  • 20
  • 22