1. 代码如下:
use strict;use file::Path;use file::Basename;#############环境变量配置区#######################my $ScanfileDir = "F:\chongqingchr\temp\";my $TargetfileDir = "F:\chongqingchr\chr\";my $SaveCHRDatalimit = 3;my $ScanPeriod = 60; #扫描周期,默认60秒my $DeleteHistoryCHRPeriod = 1440; #删除历史数据周期,默认1440个扫描周期my $Logfile = "..\Log\Log.txt";#################################################全局变量#######################my $DeleteHistoryFtpDataDay;####################################sub INITWork(){ $DeleteHistoryFtpDataDay = GetDeleteDaylimit(); &CheckLogfile();}sub CheckLogfile(){ my @StatLogfile = stat ($Logfile); my $Logfilesize = $StatLogfile[7]/1024/1024; if ($Logfilesize>50) { `del $Logfilesize`; }}sub GetTimeStampstr(){ my $timestr = localtime(time()); return $timestr."--";}sub LogMsg(){ my $Param1 = shift; my $Str = &GetTimeStampstr().$Param1; `echo $Str>>$Logfile`;}sub CreateDirIfNeed{ my $Dir = shift; if (-e $Dir and -d $Dir) { &LogMsg("The Directory $Dir is already exists!"); return 0; } &LogMsg("makepath $Dir"); eval {mkpath($Dir)}; if($@) { &LogMsg("Make path [$Dir] Failed:$@ "); return -1; } return 0;}sub MovefileToTargeDirname{ my $filename = shift; if (-e $filename and $filename =~ /(\d{4})(\d{2})(\d{2})_(\d{2})(\d{2})(\d{2})___(.*)\.dat$/) #获得dat文件创建日期 { my $TargetDir = $TargetfileDir."\"; my $Newfilename = $TargetDir."\CHR_1X_--_-.dat"; &CreateDirIfNeed($TargetDir); &LogMsg("move $filename to $Newfilename"); `move $filename $Newfilename`; }}sub MoveCHRDfiles{ my @CHRfiles = <$ScanfileDir*.dat>; print @CHRfiles; print "\n"; my $ACHRfile; foreach $ACHRfile (@CHRfiles) { &MovefileToTargeDirname($ACHRfile); }}sub GetDeleteDaylimit(){ my ($sec,$min,$hour,$day,$mon,$year,$wday,$yday,$isdst)=localtime(time()-86400 * $SaveCHRDatalimit); $day=($day<10)?"0$day":$day; $mon=($mon<9)?"0".($mon+1):($mon+1); $year+=1900; my $Now="$year$mon$day"; return $Now;}#删除指定目录sub DeleteDir{ my $Dir = shift; if (-e $Dir and -d $Dir) { &LogMsg("remove $Dir"); eval {`rd $Dir /s/q`}; if($@) { &LogMsg( "remove path [$Dir] Failed:\n$@ "); return -1; } } else { &LogMsg("The Directory $Dir is not exists"); } return 0;}#删除SaveCHRDatalimit天前的$TargetfileDir的子目录sub DeleteHistoryFtpCHRfiles(){ my @AllCHrftpSubDirs = <$TargetfileDir*>; my $ASubDir; for $ASubDir (@AllCHrftpSubDirs) { if (-e $ASubDir and -d $ASubDir) { my $DirBasename = basename $ASubDir; if ($DirBasename le $DeleteHistoryFtpDataDay) { &DeleteDir($ASubDir); } } }}#主控制流程INITWork();&LogMsg("Begin Work!");my $ScanNum = 0;while(1){ $ScanNum++; &MoveCHRDfiles(); if ($ScanNum>=$DeleteHistoryCHRPeriod) { &DeleteHistoryFtpCHRfiles(); $ScanNum = 0; } sleep($ScanPeriod);}2. 目录结构如下:
bat脚步内容如下:
%~d0cd %~p0.\Perl\bin\perl.exe "ManageCHrftp.pl"总结
以上是内存溢出为你收集整理的perl工具-转换文件名称,完善的日志机制日志全部内容,希望文章能够帮你解决perl工具-转换文件名称,完善的日志机制日志所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)