- 论坛徽章:
- 0
|
回复 1# zjm6533637
给点提示 http://www.tutorialspoint.com/perl/perl_files.htm
Working with Directories
Following are the standard functions used to play with directories.
opendir DIRHANDLE, EXPR # To open a directory
readdir DIRHANDLE # To read a directory
rewinddir DIRHANDLE # Positioning pointer to the begining
telldir DIRHANDLE # Returns current position of the dir
seekdir DIRHANDLE, POS # Pointing pointer to POS inside dir
closedir DIRHANDLE # Closing a directory.
Here is an example which opens a directory and list out all the files available inside this directory.
#!/usr/bin/perl
opendir (DIR, '.') or die "Couldn't open directory, $!";
while ($file = readdir DIR)
{
print "$file\n";
}
closedir DIR;
Another example to print the list of C source code files, you might use
#!/usr/bin/perl
opendir(DIR, '.') or die "Couldn't open directory, $!";
foreach (sort grep(/^.*\.c$/,readdir(DIR)))
{
print "$_\n";
}
closedir DIR;
You can make a new directory using the mkdir function:
To remove a directory, use the rmdir function:
To change the directory you can use chdir function. |
|