Showing posts with label Filter Files From Folder. Show all posts
Showing posts with label Filter Files From Folder. Show all posts

Monday, 23 April 2018

Java code to list all files in a directory and sub-directories

//Java code to list all files in a directory and sub-directories

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.attribute.BasicFileAttributes;

import com.csvreader.CsvWriter;

public class FilesFromDirectories {
 
    static long fileSize;
     static String size;
       
     static CsvWriter csvOutput;
     
    public static <lang> void main(String a[]) throws IOException{
     
//Folder path
    File file = new File(" D://learn");
        

//Out put csv file
    File file2 = new File("D:\\output.csv");

    if ( !file2.exists() ){
        file2.createNewFile();
    }
    csvOutput= new CsvWriter(new FileWriter(file2, true), ',');
        
        csvOutput.write("File Name");
        csvOutput.write("Creation Time");
        csvOutput.write("Size");
        csvOutput.write("Full Path");
        csvOutput.endRecord();
        listfiles(file.toString());
        
    }


public static void listfiles(String directoryName) throws IOException {
        File directory = new File(directoryName);
         BasicFileAttributes attributes = null;
        File[] fList = directory.listfilesiles();
        try
        {
        for (File f : fList) {
            if (f.isFile()) {
            attributes = Files.readAttributes(f.toPath(), BasicFileAttributes.class);
                //System.out.println("File Name ="+f.getName());
                fileSize= f.length(); 
                size=String.valueOf(fileSize);
                //System.out.println("File Size :: " + fileSize);
               // System.out.println("File Size :: " + f);
                System.out.println("File Name  :: "+ f.getName()+"Size :: "+ size + "Full Path "+f.toString());
                System.out.println("Creation Time ::"+attributes.creationTime());
        csvOutput.write(f.getName());
csvOutput.write(attributes.creationTime().toString());
        csvOutput.write(size);
    csvOutput.write(f.toString());
   
      csvOutput.endRecord();
      
              } else if (f.isDirectory()) {
                System.out.println(f);
            listfiles(f.getAbsolutePath());
            }
          
     csvOutput.flush();
        }
        }
        catch(IOException exception)
        {
        System.out.println("Exception handled when trying to get file " +
                     "attributes: " + exception.getMessage());
        }
    }
    
}

Friday, 11 March 2016

Java code to get last modified date of files from directory

//Java code to get last modified date of files from directory

import java.io.File;
import java.io.FilenameFilter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.activation.MimetypesFileTypeMap;

public class FilterFilesFromFolder {
    
   
    public static <lang> void main(String a[]){
        File file = new File("E:/sample/");
     
        //filtering txt from folder
        FilenameFilter textFilter = new FilenameFilter()
        {
            public boolean accept(File dir, String name) {
                String lowercaseName = name.toLowerCase();
                if (lowercaseName.endsWith(".txt")) {
                    return true;
                } else {
                    return false;
                }
            }
        };
       
        File[] files = file.listFiles(textFilter);
       
        long min = 0;
        String mimeType;
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        Date d = new Date();
        System.out.println("System Date :"+ sdf.format(d));
        for(File f: files){
            System.out.println("File Name ="+f.getName());
            System.out.println("Date Last Madified ="+ sdf.format(f.lastModified()));
            mimeType = new MimetypesFileTypeMap().getContentType(f);
            System.out.println("MIME Type ="+ new MimetypesFileTypeMap().getContentType(f));
           
            //getting last one hour modified files
            try
            {
                 long cd = d.getTime();
                 long lmd = f.lastModified();
                 long diff= cd-lmd;
                 System.out.println("Diff ="+diff);
                min = diff/(60 * 1000);
                 System.out.println("Diff in Min = "+min);
            }
            catch(Exception e)
            {
                System.out.println("Error in Date Diffrence = " + e);
            }
            if (min <= 60)
            {
            System.out.println("send mail");   
            }
        }
    }
}