版權聲明:本文爲博主原創文章,轉載請註明出處:https://www.cnblogs.com/sgqhappy/p/9952614.html
咱們常常遇到批量文件轉碼後壓縮打包,爲此我編寫了一個Perl腳本,運行腳本文件後自動實現這些功能。html
1.定義數組
#!/usr/bin/perl use strict; my @array=( "110", "120", "130", "140", "150", "210", "212", "220", "230", "310", "320", "330", "332", "340", "350", "352", "360", "370", "372", "410", "420", "430", "440", "443", "450", "460", "500", "510", "520", "530", "540" );
2.讀取文件夾內容
my $utf8_path = "/tmp/"; #count the files. my $file_count=0; #get files_name. my @file_name_array; opendir(DIR,$utf8_path) or die "can't open the directory!"; @file_name_array=readdir DIR;
3.操做文件夾下的文件
這裏運用了正則表達式,判斷文件夾下的文件是否符合要求,若是符合,則進行後面的計算。正則表達式
實現文件計數,文件名處理,轉碼等功能。數組
foreach my $file_name (@file_name_array){ if($file_name=~/.*_M_0000_0000/){ #print the file count. $file_count++; print "File number: ".$file_count."\n"; #change files name my $mv="mv "."utf8_path"."/$file_name"." $utf8_path"."/$file_name".".utf8"; `$mv`; print "$mv"."\n"; #file_name from lower to upper. my $file_name_upper=uc(substr($file_name,0,(length($file_name)-4))).".txt"; print "$file_name"."\n"; print "$file_name_upper"."\n"; #This is data cat from utf8 to gbk. my $cat="cat "."$utf8_path"."$file_name"." | iconv -f utf8 -t gbk -c > "."$utf8_path"."$file_name".".txt"; #This is prompt message. print "cat "."$file_name"." success!"."\n\n"; }else{ print "$file_name "."is not the kind of file type you want!\n\n"; } }
4.將文件打tar包
#This is tar function. my $tar="tar -zcvf "."gz.tar"." *.txt"; `$tar`; print "$tar"." success!\n";
5.將tar包放到指定路徑下
#loop every number,tar and move files to 36 directories. foreach my $number(@array){ #define path. my $gbk_path="/data/u"."$number"."/test/"; my $cp="cp gz.tar "."$gbk_path"; my $cp2="cp dl.tar "."$gbk_path"; `$cp`; `$cp2`; print "$cp"." success! "."\n"; print "$cp2"." success! "."\n"; }; print "============$file_count"." files cat"." success!!!=============\n";
做者:sgqhappy
出處:https://www.cnblogs.com/sgqhappy/
本文版權歸做者和博客園共有,歡迎轉載,但未經做者贊成必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接。app