Click here to Skip to main content
15,939,960 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to delete multiple folders,subfolders of last week in java

Ex. Currently I am creating a folder(parent directory) in HH_DD_MONTH_results (ex. 22_06_November_results) format and subdirectory in HHhour format

Problem: Currently I have current week & last week folders and subfolders ,now I want to delete last week created folder & sub folders, how I can delete only those folder ??

I have prepared following code which is currently creating directory and subdirectories and delete all of them but not to time specific

Please suggest me solution about my problem

package mkdirJava;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class Check2
{
public static String givePath;


public static void takepath()
{
Date date=new Date();
int dd=date.getDate();
int mm=date.getMonth();
int hh=date.getHours();
Date dateq=new Date();
Calendar c = Calendar.getInstance();
String month=c.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH );
String dirname =hh+"_"+dd+"_"+month+"_results";
String subdir = hh+"hour";
File parentFile = new File("C:\\EclipseJDk\\mkdirJava\\images");
String papa=parentFile.getAbsolutePath().toString();
for(File f4:parentFile.listFiles())
{

System.out.println(f4);
}
// List<string>abc3= new ArrayList<string>();
File f1 = new File("C:\\EclipseJDk\\mkdirJava\\images\\"+dirname);

if(!f1.exists())
{
f1.mkdirs();

}
File f2 = new File(f1.getAbsolutePath()+"\\"+subdir);
f2.mkdir();
givePath = f2.getAbsolutePath().toString();
System.out.println(givePath);
deleteDir(parentFile);
}



public static void deleteDir(File parentFile )
{

for(File f5:parentFile.listFiles())
{
for(File mainfile:f5.listFiles())
{
for(File subfile: mainfile.listFiles())
{
subfile.delete();
}

f5.delete();
}

F5.delete();
}
}




public static void main(String args[])
{
takepath();
}


}
Posted
Comments
Richard MacCutchan 6-Nov-15 12:21pm    
Just enter the date/time string that corresponds to the filenames you wish to delete and build all the names as required.
Member 12120023 7-Nov-15 5:26am    
Can you please suggest me in sample code what you are suggesting,please
Richard MacCutchan 7-Nov-15 6:19am    
I mean that you need to create some way of entering the data values into your program. Or create some algorithm for deciding how to select the files that need to be deleted. Writing the code is the easy part, but you have to have a set of rules first.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900