由于项目需要,最近在学习Perl。很强大的东西,方便的文件读写,强大的字符串处理,随心所欲的语法格式,还有一个CPAN。
借了本精通正则表达式,一边学一边写。
早上还写了个脚本,放到启动项里,删除临时文件什么的,哈哈。
#
!perl -w
#程序说明:Windows下,安装过ActivePerl的,可以把这个文件放在
#启动项里,每次启动都会清理Temp文件和最近的文档访问记录
#系统装在C盘的才可以用。。。
#代码大家自己看看吧,也不是很难。
use
File
::
Path;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
sub
get_cur_username() {
my
$cur_username
;
$cur_username
=
`set USERNAME`;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
$cur_username
=~
/
USERNAME
=
(
.*
)
/
;
$cur_username
=
$
1
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
return
$cur_username
;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
$usr
=
get_cur_username();
push
(
@paths
,
'
C:Documents and Settings/
'
.
$usr
.
'
Recent/
'
);
push
(
@paths
,
'
C:Documents and Settings/
'
.
$usr
.
'
Local SettingsTemp/
'
);
push
(
@paths
,
'
C:WINDOWSTemp/
'
);
#
push (@paths,'C:Documents and Settings/'.$usr.'Local SettingsTemporary Internet Files/');
foreach
$path
(
@paths
) {
opendir
(DIR
,
$path
) or
die
"
Cannot open The DIR: $!
"
;
while
(
my
$name
=
readdir
DIR)
{
$filename
=
$path
.
"
$name
"
;
print
$filename
.
"
"
;
if
(
$filename
!~
/.?.
$
/
)
{
if
(
-
f
$filename
)
{
unlink
$filename
or
print
"
Can't delete :$!
"
;
}
else
{
rmtree
$filename
;
#
or print "Can't delete :$! ";
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
}
}
考虑用这个去分析Ogame的积分文件。。。恩恩。。。应该很好用,很华丽。。。
那天用来整理ORC录像文件,查找重复文件使用了MD5,导出CSV以后用Excel查找重复的MD5,很好用。
#
目录的深度优先遍历操作
#! perl -w
use
File
::
Find;
print
"
请输入一个目录,不要太大
"
;
chomp
(
$inputDir
=
<
STDIN
>
);
open
(FILEOUT
,
'
>>F:orc.log
'
);
my
@dir_array
;
push
@dir_array
,
$inputDir
;
open
(ORCDOCS
,
'
>>F:orcfiles.log
'
);
open
(CSV
,
'
>>F:orc.csv
'
);
#
print FILEOUT "push DIR : $inputdir ";
while
(
my
$popdir
=
pop
@dir_array
) {
opendir
DIR
,
$popdir
or
print
"
Can't open dir $popdir:$!
"
;
while
(
my
$file
=
readdir
DIR) {
if
(
'
.
'
eq
$file
||
'
..
'
eq
$file
) {
next
; }
#
拼接文件路径
$file
=
"
$popdir
"
.
'
/
'
.
"
$file
"
;
if
(
-
d
$file
) {
push
@dir_array
,
$file
;
#
print FILEOUT "push DIR : $file ";
print
"
DIR: $file
"
;
#
print FILEOUT "DIR : $file ";
}
else
{
print
"
File: $file
"
;
if
(
$file
=~/
orc$
/
i) {
print
ORCDOCS
"
$file
"
;
print
FILEOUT
$file
.
"
"
;
print
CSV
"
$file
"
.
"
,
"
;
open
(FILEORC
,
$file
) or
die
"
Error on Opening ORC FILE: $!
"
;
$orcfile
=
<
FILEORC
>
;
while
(
$orcfile
)
{
print
FILEOUT
$orcfile
;
$orcfile
=
<
FILEORC
>
;
}
close
FILEORC;
use
strict;
use
warnings;
use
Digest
::
MD5;
my
$md5
=
Digest
::
MD5
->
new();
open
( HFILE
,
$file
) or
die
(
"
Can't open:$!
"
);
binmode
( HFILE);
#
这一行也不能少
$md5
->
addfile(
*
HFILE);
#
不要忘记*号哦
close
( HFILE);
my
$stringmd5
=
$md5
->
hexdigest()
.
"
"
;
print
ORCDOCS
$stringmd5
;
print
CSV
$stringmd5
;
$md5
->
reset
();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
}
}
closedir
DIR;
print
"
CloseDir
"
;
}
继续学习。。。