MShoaib has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am trying to pick files and directories from multiple directories and folders for further processing in my programme logic. I am able to pick majority of the files and directories. However, with -f and all other remaining file operators (that I have already tried so far), I am unable to pick following type of files from the cache folder.

1: /(a directory)/4-http???wscont1.apps.microsoft.co?winstore?1.8x?2d60181a-b6d7-499a-9414-f07b0f932419?AppTile.1.396657.404061.png

2: /(a directory)/4-https???wscont.apps.microsoft.com?winstore?6.3.0.1?100?GB?en-us?MS?467?features1de1406b-b4ec-4d86-9066-68bf9c5d67f2.json

3: (another file path)etc.

Can someone advise, which code to use to pick this type of files being present in 'a directory'?

I am using following code:-

```
for (@files) { $element = $_; if (-d $_) { push @directories, process_files ($_); ++$dir_counter; } elsif (-f $_) { ++$file_counter; next; } elsif ($_ =~ /\.dat$/i || $_ =~ /\.png$/i) { ++$file_counter; next; } else { print NOW "I am in else statement: process_files($_)\n"; } # else loop end
```

Replies are listed 'Best First'.
Re: File types not being picked up by script
by ysth (Canon) on Jul 06, 2025 at 17:41 UTC
    What do you mean by "pick up", that both -f and -d are false? What is join(':',stat $_)?

    You don't show the code that sets @files; a common mistake in directory recursion is forgetting to add the directory to the beginning of the path. There are other common problems too; you really should use one of the File::Find modules for this.

      Hi, by pick up I mean to say that, the code finds all of the directories and files correctly, except the ones I listed that it does not.

      I dont have the join(':',stat $_) in my code.

      As per your comment, I have added the code below on how I set the @files

      ```
      my @files; my $path = shift; opendir (DIR, $path) or die "Unable to open $path: $!"; my @files = # Third: Prepend the full path map { $path . '\\' . $_ } # Second: take out '.' and '..' grep { !/^\.{1,2}$/ } # First: get all files readdir (DIR); closedir (DIR);
      ```
        I am not sure exactly what you are trying to do but as has been mentioned File::Find is the right tool for this job. This is some untested code for your pleasure...
        use strict; use warnings; use File::Find; my $starting_dir = "put path to dir here"; my $count_dat_png = 0; find(\&wanted, $starting_dir); print "number of dat and png files: $count_dat_png\n"; sub wanted { return if -d ; # skip directories # . and .. are directories # or just leave this out entirely if (-f and ( /\.dat$/i or /\.png$/i)) { #print "$File::Find::name\n"; #full name if you want $count_dat_png++; } }
        added: I would read the example and explanation of the module File::Find carefully.