简体   繁体   中英

Code for finding the files numbers missing in a folder

I need to identify the file numbers which are missing in a folder.

I have retrieved the files names by using the code below :

File folder = new File(FILE_PATH);
File[] listOfFiles = folder.listFiles();

for (int i = 0; i < listOfFiles.length; i++) {
    if (listOfFiles[i].isFile()) {
        System.out.println("File " + listOfFiles[i].getName());
    } else if (listOfFiles[i].isDirectory()) {
        System.out.println("Directory " + listOfFiles[i].getName());
    }
 }

But now after retrieving i need to find which are the file number which are missing from a file range of 1-1976 both included.

If you need just the filenames, you may use list() method. After you get all the filenames with this method, you can just check the presence of the specified filenames, like:

File parent = ...
String prefix = "xxx_", suffix = ".txt"; // for example

Set<String> files = new HashSet<>(Arrays.asList(parent.list()));

// or, as suggested by @JulienLopez:

String pattern = Pattern.quote(prefix) + "\\d+" + Pattern.quote(suffix);
Set<String> files = new HashSet<>(Arrays.asList(parent.list((dir, file) -> file.matches(pattern))));

for (int i = 1; i <= 1976; ++i) { // actually constant should be used
    if (!files.contains(prefix + i + suffix)) {
        System.out.format("File #%d doesn't exist%n", i);
    }
}

But if you really need to check, that the file is not, for example, the directory, there's one more way to do it, by just creating the File s for every i and checking its existence:

for (int i = 1; i <= 1976; ++i) {
    File file = new File(parent, prefix + i + suffix);

    if (!file.isFile()) {
        System.out.format("File #%d doesn't exist or is directory%n", i);
    }
}

I'm not sure your structural of your file name , and what exactly on your mind with "both included". That is my idea,I hope it's a bit help for you.

String FILE_PREFIX= "your_file_prefix"; // Your file prefix. If your file is "logfile_on_20160121_0001" then the prefix is "logfile_on_20160121_"
int RANGE_MIN = 1;
int RANGE_MAX = 1976;
int fileList[] = new int[RANGE_MAX];
int directoryList[] = new int[RANGE_MAX];

// Quote your code with a bit modify from me
File folder = new File(FILE_PATH);
File[] listOfFiles = folder.listFiles();
    for (int i = 0; i < listOfFiles.length; i++) {
      if (listOfFiles[i].isFile()) {
        System.out.println("File " + listOfFiles[i].getName());
        // Added started
        String tempSplitedName[] = listOfFiles[i].split(FILE_PREFIX);
        if(tempSplitedName.length==2){
            int seq = Integer.parseInt(tempSplitedName[2]);
            if(seq>=RANGE_MIN && seq<=RANGE_MAX){
                fileList[seq] = 1;
            }
        }
        // Added ended
      } else if (listOfFiles[i].isDirectory()) {
        System.out.println("Directory " + listOfFiles[i].getName());
        // Added started
        String tempSplitedName[] = listOfFiles[i].split(FILE_PREFIX);
        if(tempSplitedName.length==2){
            int seq = Integer.parseInt(tempSplitedName[2]);
            if(seq>=RANGE_MIN && seq<=RANGE_MAX){
                directoryList[seq] = 1;
            }
        }
        // Added ended
      }

// Now you count missing files/directory, which is equal 0
for (int i=RANGE_MIN; i<=RANGE_MAX; i++){
    if(fileList[i]==0) System.out.println("Missing file No." + i);
}
for (int i=RANGE_MIN; i<=RANGE_MAX; i++){
    if(directoryList[i]==0) System.out.println("Missing directory No." + i);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM