Wednesday, October 11, 2023

EM-Tirupati Codeathon Series #07

[Question]

WINDOWS DIRECTORY SEARCH

Your program will accept a valid windows directory as theinput. If the directory is not valid, you have to show a message saying that 'Directory Not Found on the Filesystem Else, you have to find out all txt files and .exe files in your directory and then add it to a java collection in such a way that it stores it in a sorted way (sorted by the directory name) where the key is the fully qualified directory name and the values must be the list of all .txt and .exe files. You have to do the same for all child directories that are found in the parent directory, until the time no directories remain to traverse.

Filesystem (Sample)

c:\files

      file1.txt

      file2.exe

      file3.bat

  \filex

           \filez

\filey

       file 4.txt

       file5.exe

       \filef

file6.txt

 file7.exe

 \fileg

[Sample Input]

 c:\files

[Sample Output]

c:\filesfile1.txt, file2.exe

c:\files\filex                             

c:\files\filexz\filez                      

c:\files\filey       file4.txt, file5.exe                       

c:\files\filey\fileffile6.txt, file7.exe

c:files\filey\filef\fileg

Explanation of the Output:

You have to make sure that you traverse every sub-directory that is inside the input directory and list the .txt and .exe in the format as shown above. You have to make sure no sub-directory should be left out and that all levels of child sub-directories are traversed. If no matching file is found, then a blank output should be printed as shown above. Please strictly stick to the output format as given above.

Code Explaination:

  • The program is structured to collect specific file types within a specified directory and its subdirectories.
  • User Input:
    • The program starts by taking user input for a valid Windows directory path.
    • Check Directory Existence:
      • It checks if the specified directory exists on the filesystem and is indeed a directory. If not found, it displays an error message and exits.
    • Initialize Data Structures:
      • The program initializes a TreeMap called fileMap to store directory-path-file lists.
    • Define File Filtering Criteria:
      • An interface FileFilter is introduced for file filtering conditions.
      • A specific filter (txtAndExeFileFilter) is defined to accept files with .txt and .exe extensions.
    • Collect Files:
      • The program uses a recursive method collectFiles to navigate through directories and collect files that match the filter.
      • It stores the directory path and a list of filtered files in fileMap.
    • Display Results:
      • Finally, the program displays the collected data. It lists each directory and the associated files that match the filter criteria.

    GitHub Repo link: http://surl.li/lyiqn   


    Source Code(Java):

    import java.io.File;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    import java.util.Scanner;
    import java.util.TreeMap;

    // Interface for file filtering
    interface FileFilter {
        boolean accept(File file);
    }

    public class Question07_Venkata {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            System.out.print("Enter a valid Windows directory path: ");
            String directoryPath = scanner.nextLine();
            scanner.close();

            File directory = new File(directoryPath);

            if (!directory.exists() || !directory.isDirectory()) {
                System.out.println("Directory Not Found on the Filesystem");
                return;
            }

            Map<String, List<String>> fileMap = new TreeMap<>();
            // Implement a file filter for .txt and .exe files
            FileFilter txtAndExeFileFilter = file -> file.isFile() && (file.getName().endsWith(".txt") || file.getName().endsWith(".exe"));

            collectFiles(directory, fileMap, txtAndExeFileFilter);

            for (Map.Entry<String, List<String>> entry : fileMap.entrySet()) {
                System.out.println("Directory: " + entry.getKey());
                System.out.println("Files: "+entry.getKey()+":  " + entry.getValue());
            }
        }

        private static void collectFiles(File directory, Map<String, List<String>> fileMap, FileFilter fileFilter) {
            File[] files = directory.listFiles();
            if (files != null) {
                List<String> filteredFiles = new ArrayList<>();
                for (File file : files) {
                    if (fileFilter.accept(file)) {
                        filteredFiles.add(file.getName());
                    }
                }
                if (!filteredFiles.isEmpty()) {
                    fileMap.put(directory.getAbsolutePath(), filteredFiles);
                }

                for (File subDirectory : files) {
                    if (subDirectory.isDirectory()) {
                        collectFiles(subDirectory, fileMap, fileFilter);
                    }
                }
            }
        }
    }

     

    Thank you

    Venkata kishore T(Intern)

    Shield Warriors,

    Data Shield Team,

    Enterprise Minds.

    No comments:

    Post a Comment

    EM-Tirupati Codeathon Series #08

    [Question] ROBOTIC CRICKET MATCH you should write a program that simulate an automatic cricket match between India and Sri Lanka. The fo...