Perl文件搜索脚本脚本安全 -电脑资料

电脑资料 时间:2019-01-01 我要投稿
【meiwen.anslib.com - 电脑资料】

   

    好久没有写些东西了,

Perl文件搜索脚本脚本安全

。。。最近一直在用PERL写一些有趣的程序,几乎每天都有新的程序产生,实在是太有趣了!今天又写了一个用来搜索文件的小程序,虽然我以前也写过类似的,但是方法很繁琐,这次用到了一个很方便的模块:File::Find!代码明显变得简短多了,其中还加了一些自己的想法进去,可以自动修改Windows当中输入的错误,举个简单的例子,比如:程序提示输入一个查找路径,假设输入“C”,程序会自动把它改成“C:”,这样方便查找。还有,在Windows系统中,输入“/”和“\”是都支持的,这样会导致打印出的结果显得很凌乱,如:C:\\Perl//searchMyFile.pl,程序都会统一将其改成类似Unix系统中的“/”的形式,如:C:/Perl/searchMyFile.pl。还有很多自动修改的功能,都是用到了PERL强大的正则表达式,不多说了,我把我的代码复制上来,如果大家有兴趣可以帮忙纠正:-)

    #!/usr/bin/perl

    use strict;

    use warnings;

    use Cwd;

    use File::Find;

    ####################################################################

    File name        : searchMyFile.pl

    Written by       : B.S.F

    Last modified  : 05/18/2013

    Description      : Easy way to search some file(s) with a keyword!

    ####################################################################

    # Input a path to search...

    print "Enter a path: ";

    chomp(my $path = <>);

    # Change "~" or "~/" to your home directory on Linux/FreeBSD platform.

if(($^O eq "linux") || ($^O eq "freebsd")) {

    $path =~ s/~|~\//$ENV{'HOME'}/;

    }

    # Deal with some format of path on Windows NT platform.

    else {

    # Change "C" to "C:", etc.

    $path .= ":" if $path =~ /^\w$/;

    # Change "C:\\" to "C:", etc.

    $path =~ s/\W+/:/ if $path =~ /^\w\W+$/;

    # Change "C:\\Perl\" to "C:\\Perl", etc.

    $path =~ s/\W+$// if $path =~ /^\w\W+\w+/;

    # Change "C:\\Perl" to "C:/Perl", etc.

    $path =~ s/\W+/:\// if $path =~ /^\w\W+\w+/;

    }

    # Test if can enter into the path or not, if not, terminated!

    chdir($path) or die "Couldn't get into $path: $!";

    # Input some keyword...

    print "Enter a keyword: ";

    chomp(my $key = <>);

    # Change the "." character to the actually meanings, matching some postfix.

    # Such as ".jpg", ".txt", ".doc", etc.

    $key =~ s/\./\\./;

    # Print some information...

    print "Searching under \"", getcwd, "\"...\n";

    #

    # Searching...

    #

    sub search {

    # Get rid of the directory matches "System Volume Information"(some boring stuff on Windows...).

    $File::Find::prune = 1 if /System Volume Information/;

    # Open the "i" switch to mach more,  such as ".JPG" or ".jpg"...

    if (/$key/i) {

    # "" stand for a directory, "" for a file!

    my $target = (-d) ? "  $File::Find::name" : "  $File::Find::name";

    # A neat layout for Windows...

    $target =~ s/:/:\// if $target !~ /\//;

    print "$target\n";

    }

    }

    # Begin to search...

    find(\&search, $path);

最新文章