上海龙凤1314 shlf

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

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

   

    好久没有写些东西了,

Perl文件搜索脚本脚本安全

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

上海龙凤1314 shlf    #!/usr/bin/perl

上海龙凤1314 shlf    use strict;

    use warnings;

上海龙凤1314 shlf    use Cwd;

上海龙凤1314 shlf    use File::Find;

上海龙凤1314 shlf    ####################################################################

    File name        : searchMyFile.pl

    Written by       : B.S.F

上海龙凤1314 shlf    Last modified  : 05/18/2013

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

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

    # Input a path to search...

上海龙凤1314 shlf    print "Enter a path: ";

    chomp(my $path = <>);

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

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

上海龙凤1314 shlf    $path =~ s/~|~\//$ENV{'HOME'}/;

    }

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

上海龙凤1314 shlf    else {

上海龙凤1314 shlf    # Change "C" to "C:", etc.

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

上海龙凤1314 shlf    # Change "C:\\" to "C:", etc.

上海龙凤1314 shlf    $path =~ s/\W+/:/ if $path =~ /^\w\W+$/;

上海龙凤1314 shlf    # Change "C:\\Perl\" to "C:\\Perl", etc.

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

上海龙凤1314 shlf    # Change "C:\\Perl" to "C:/Perl", etc.

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

上海龙凤1314 shlf    }

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

上海龙凤1314 shlf    chdir($path) or die "Couldn't get into $path: $!";

上海龙凤1314 shlf    # Input some keyword...

上海龙凤1314 shlf    print "Enter a keyword: ";

    chomp(my $key = <>);

上海龙凤1314 shlf    # Change the "." character to the actually meanings, matching some postfix.

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

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

上海龙凤1314 shlf    # Print some information...

上海龙凤1314 shlf    print "Searching under \"", getcwd, "\"...\n";

    #

    # Searching...

    #

    sub search {

上海龙凤1314 shlf    # Get rid of the directory matches "System Volume Information"(some boring stuff on Windows...).

上海龙凤1314 shlf    $File::Find::prune = 1 if /System Volume Information/;

上海龙凤1314 shlf    # Open the "i" switch to mach more,  such as ".JPG" or ".jpg"...

上海龙凤1314 shlf    if (/$key/i) {

    # "" stand for a directory, "上海龙凤1314 shlf" for a file!

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

    # A neat layout for Windows...

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

上海龙凤1314 shlf    print "$target\n";

上海龙凤1314 shlf    }

    }

    # Begin to search...

上海龙凤1314 shlf    find(\&search, $path);

最新文章