Perl文件夹操作

2320阅读 0评论2013-05-24 CUTianrui007
分类:其他平台

use strict;
my $CPPDir;
opendir($CPPDir,"D:\\GenCPPFile") || die("Cna not open dir:$!");
#得到的列表是包含.和..的,也就是i当前文件夹和父文件夹,所以要用grep去除这两项
my @TmpAllFiles=readdir($CPPDir);
#grep的本质就是for循环
my @AllFiles=grep(!/^\.\.?$/,@TmpAllFiles);
@AllFiles=grep(/\.cpp{0,1}/,@AllFiles);
my $FilePath;
my $FileHandle;
my $NewFileHandle;

chdir("D:\\GenCPPFile") || warn("D:\\GenCPPFile does not exist");
unlink <*.'p'>; #m删除文件,其前提是切换工作目录,因为默认目录为Perl目录
#system("del *.'p'");
foreach $FilePath (@AllFiles)
{
    open($NewFileHandle,">D:\\GenCPPFile\\$FilePath.'p'");
    open($FileHandle,"D:\\GenCPPFile\\$FilePath")|| die("Cna not open dir:$!");;
    while(<$FileHandle>)
    {
        #$_=~/^\s*$/查找空行
        if(($_=~/^\s*$/) || ($_=~/#line/) || ($_=~/__cdecl/) || ($_=~/extern/))
        {
           
        }
        else
        {
            print $NewFileHandle $_;
        }
    }
    close($NewFileHandle);#一定要关闭句柄,否则下面的system是不成功的
    close($FileHandle);
}

closedir($CPPDir);

system("del *.cpp");#system命令的前提是chdir进行目录切换
system("rename *.'p' *.cpp");
system("rename *.cpp *.");

 

上一篇:VC笔记
下一篇:一点编程感悟