I am running Perl in Windows and I am getting a list of all the files in a directory using readdir and storing the result in an array. The first two elements in the array seem to always be "." and "..". Is this order guaranteed (assuming the operating system does not change)?
I would like to do the following to remove these values:
my $directory = 'C:\\foo\\bar';
opendir my $directory_handle, $directory
or die "Could not open '$directory' for reading: $!\n";
my @files = readdir $directory_handle;
splice ( @files, 0, 2 ); # Remove the "." and ".." elements from the array
But I am worried that it might not be safe to do so. All the solutions I have seen use regular expressions or if statements for each element in the array and I would rather not use either of those approaches if I don't have to. Thoughts?
my @files = grep { $_ ne '.' && $_ ne '..' } readdir $dh;
(although any Perl developer should be able to understand simple regexes). – Septal,
D:\ `, ...). (Pardon the backticks, I can't make that come out the way I want it.) – Fleetingreaddir()
function: https://mcmap.net/q/55147/-does-readdir-guarantee-an-order/827263 – FleetingC:
definitely. From my brief testing, it appears actual disks don't have them, network shares mounted as drives do. – ToreadorC:
is not necessarily the only mounted device. Mounting a second drive asD:
is not uncommon -- and remember that it's calledC:
becauseA:
andB:
were commonly floppy drives. Windows, unlike Unix, has no single root directory. – Fleeting.
and..
in Windows. (e.g.subst z: c:\users & dir z:\ & subst /d z:
) – Linette