Perl Implementation Traversal Directory example _perl

Source: Internet
Author: User


Environment for WinDOS, two points to note:


1, the default ActivePerl under the code is GBK, so you need to convert the string to GBK will not display garbled
2, to traverse the file directory, you need to exclude a special directory.


Complete code:
Copy Code code as follows:

#!/usr/bin/perl
Use strict;
Use warnings;
Use Encode qw/from_to/;

My $path = "e:/css design";
My $filecount = 0;

Sub Parse_env {
My $path = $_[0]; #or use my ($path) = @_; @_ similar to arguments in JavaScript
My $subpath;
My $handle;

If (-D $path) {#Does the current path be a directory?
If (Opendir ($handle, $path)) {
While ($subpath = Readdir ($handle)) {
If (!) ( $subpath =~ m/^\.$/) and! ($subpath =~ m/^ (\.\.) $/)) {
My $p = $path. " /$subpath ";

If (-D $p) {
Parse_env ($p);
} else {
+ + $filecount;
Print $p. " \ n ";
}
}
}
Closedir ($handle);
}
}

Return $filecount;
}

My $count = parse_env $path;
My $str = "Total Files:". $count;
From_to ($str, "UTF8", "GBK");

Print $str;

Run Effect chart:


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.