This utlity
program reads the names of all files in a folder and generate a
report enlisting filenames based on selected file extensions only via
java program. This standalone program extracts names of files with
.doc extension. The program can be customized to enable export of
filenames into an excel. all file names or files with .xls or .pdf
extension can be enlisted in an excel.
1. Add the JXl.jar
file to the classpath of the project.
2. Add the java
file in a project(change pachakge name in applicable)
3. Change the
source folder location and if required, name of the excel file to be
generated.
4. Run the
GetFolderContent.java file
Excel file will be
generated in the corresponding project folder.
import java.io.File; import java.io.IOException; import java.util.ArrayList; import jxl.Workbook; import jxl.WorkbookSettings; import jxl.write.Label; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.WriteException; import jxl.write.biff.RowsExceededException; public class GetFolderContent { private static String excelFileName = "FileList.xls"; private static String targetexcelFilepath = "C:\backup\test\Test_Work"; // generated excel file location private static String sourceFolderPath = "C:\DUK-CC"; // source Directory private static int totalFileCount = 0; // variable to store total file count private static int fileCount = 0; // variable to store required files count private static ArrayList<String> aList = new ArrayList<String>(); /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub // to get file names File folder = new File(sourceFolderPath); File[] listOfFilesInDirectory = folder.listFiles(); try { for (int numberOfFilesInFolder = 0; numberOfFilesInFolder < listOfFilesInDirectory.length; numberOfFilesInFolder++) { if (listOfFilesInDirectory[numberOfFilesInFolder].isFile()) { totalFileCount++; String files = listOfFilesInDirectory[numberOfFilesInFolder] .getName(); if (files.endsWith(".doc") || files.endsWith(".DOC") || files.endsWith(".DOCX") || files.endsWith(".docx")) { aList.add(files); fileCount++; } } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("Total file count = " + totalFileCount); System.out.println("Required file count = " + fileCount); createExcelFileAndGetContent(); } // code to generate an excel file public static void createExcelFileAndGetContent() { File file = new File(excelFileName); try { WorkbookSettings wbSettings = new WorkbookSettings(); WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings); workbook.createSheet("File Names", 0); System.out.println("excel File is created in path -- " + targetexcelFilepath+ "n*********Items in directory:**********" ); WritableSheet excelSheet = (WritableSheet) workbook.getSheet(0); if (!aList.isEmpty()) { for (int rowNumber = 0; rowNumber < aList.size(); rowNumber++) { System.out.println(aList.get(rowNumber)); Label label = new Label(1, rowNumber, (String) aList.get(rowNumber)); excelSheet.addCell(label); } } else { System.out.println("No matching files found"); } workbook.write(); workbook.close(); } catch (RowsExceededException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IndexOutOfBoundsException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (WriteException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }