Skip to content
Advertisement

How to move files to year/month/day folders based on timestamp in java [closed]

I have files with timestamp as its fileName. I want to move these files into years/month/day folder based on it timestamp which is nothing but the fileName.

Eg: fileName=2017-11-2_23-59-59(YYYY-mm-dd_HH-MM-SS). Now I want to move this file to 2017 folder and inside that folder 11 (month) folder and inside that 2 (date) folder and if the folder is not present create one.

So all days folders should be inside a month folder and months folders should be inside year folder. This is the folder structure i need.

Advertisement

Answer

This might be helpful for you:

String file = form.getFile().getOriginalFilename();
String[] dateParts = file.split("-");
String year = dateParts[0]; 
String month = dateParts[1]; 
String day = dateParts[2]; 
String UPLOADED_FOLDER = "C://Users//pandeyv//Desktop//"+year+"//"+month+"//"+day+"//";
File dir = new File(UPLOADED_FOLDER);
if (!dir.exists())
    dir.mkdirs();
7 People found this is helpful
Advertisement