How do I list the files in a directory?
2016-12-25 22:27
525 查看
原文地址:How do I list the files in a directory?
You want a list of all the files, or all the files matching a certain pattern, or with a certain ending, in a directory
See further down for a more compact way of doing this.
You want a list of all the files, or all the files matching a certain pattern, or with a certain ending, in a directory
The solution
Reading directories is a bit like reading files. First you open the directory, then you read from it and then you close it. You use a directory handle much as you use a file handle.Step 1: Opening the directory
To open the directory, we use a function calledopendir. You use this much like the
openfunction to open files. In the example below, we open the /tmp directory:
#!/usr/bin/perl use strict; use warnings; my $directory = '/tmp'; opendir (DIR, $directory) or die $!;
Step 2: Reading the directory
To read the files and directories in the directory we use thereaddirfunction.
readdirreturns the name of each file or directory in the opened directory in turn when used in scalar context, or a list of the names of all files and directories in that directory when used in list context. This means that we can use
readdirin a foreach loop (or any other loop construct):
while (my $file = readdir(DIR)) { print "$file\n"; }
Step 3: Closing the directory
We use the functionclosedirto close the directory once we are finished with it. Like files, the directory will be closed when the program terminates, but sometimes you will need to explicitly close the directory:
closedir(DIR);
Directory Listing
Provided your program has sufficient access to the directory being read,readdirwill list every file and directory contained in that directory. However, you often will not want all files or directories. For example, it is quite common to exclude all filenames beginning with a period:
#!/usr/bin/perl use strict; use warnings; my $dir = '/tmp'; opendir(DIR, $dir) or die $!; while (my $file = readdir(DIR)) { # Use a regular expression to ignore files beginning with a period next if ($file =~ m/^\./); print "$file\n"; } closedir(DIR); exit 0;
See further down for a more compact way of doing this.
Find the directories
Sometimes you may want to find all the directories in a directory. Remember thatreaddir()gives you the names of the files and directories, not the paths. If you want to test a file using any of the standard file tests, you need to use the full path:
#!/usr/bin/perl use strict; use warnings; my $dir = '/tmp'; opendir(DIR, $dir) or die $!; while (my $file = readdir(DIR)) { # A file test to check that it is a directory # Use -f to test for a file next unless (-d "$dir/$file"); print "$file\n"; } closedir(DIR); exit 0;
Find files ending in...
Quite often you want to find all files ending in a given suffix. For example, you may want to find all files ending in.txt:
#!/usr/bin/perl use strict; use warnings; my $dir = '/tmp'; opendir(DIR, $dir) or die $!; while (my $file = readdir(DIR)) { # We only want files next unless (-f "$dir/$file"); # Use a regular expression to find files ending in .txt next unless ($file =~ m/\.txt$/); print "$file\n"; } closedir(DIR); exit 0;
An advanced example
A more advanced example is to usegrepto filter out the files you want. The following example (based on a code sample from
perldoc -f readdir) gets all the files (not directories) beginning with a period from the open directory. The filenames are found in the array
@dots.
#!/usr/bin/perl use strict; use warnings; my $dir = '/tmp'; opendir(DIR, $dir) or die $!; my @dots = grep { /^\./ # Begins with a period && -f "$dir/$_" # and is a file } readdir(DIR); # Loop through the array printing out the filenames foreach my $file (@dots) { print "$file\n"; } closedir(DIR); exit 0;
File::Find
You can use the File::Find module to recursively search through a directory (or directories). It is best used when you want to perform some operation on each file. Seeperldoc File::Findfor more information.
glob
Another way of getting a directory listing - if you're only interested in the current directory and not in any sub-directories - is to useglob. You can pass
globa pattern (or patterns) to match and it will return any files that match. The example below will list all .pl and .pm files in the current directory:
#!/usr/bin/perl use strict; use warnings; my @files = glob("*.pl *.pm"); foreach my $file (@files) { print "$file\n"; } exit 0;
See also
perldoc -f opendir perldoc -f readdir perldoc -f closedir perldoc -f -X perldoc -f grep perldoc File::Find perldoc -f glob perldoc File::Glob
相关文章推荐
- How do I change the directory that MATLAB is initially started in?
- I want to do custom handling of special keys such as the Tab key or F2 in the TextBox of a column in the DataGrid. How do I subc
- How to view the schema class or attribute definition you first have to install the Active Directory Schema snap-in
- How to do the TM symbol in HTML
- How do I change the default runlevel in Feodra 15?
- How to do live debug the Managed code in Windows Phone 7
- Keep the number of files in your /app_code directory small
- FAQ How do I write to the console from a plug-in?
- Magento: How to list all the products in a single category in a sidebar block
- How do I use the DCMTK libraries in my own applica
- open files in a directory and then find someword in the text tcl tk
- How do Download file in the asp.net
- How To Create a Child Domain in Active Directory and Delegate the DNS Namespace to the Child Domain
- How to change the name in open with list / File association.
- How Do use the ImageButton in DataGrid...
- how to do with the special characters in the xml data
- JDeveloper JSP files must reside in the server root directory or a subdirectory
- How do I write a LINQ to Entities query which has the equivalent of the SQL “in” keyword?
- How do you remove the duplicate characters in a given string without using any additional buffer.
- How are the icon files in my application bundle used on iPad and iPhone?