廣州代做網(wǎng)站二維碼推廣賺傭金平臺
perl讀取目錄,寫入文件
此腳本有兩個輸入?yún)?shù),第一個參數(shù)為需要打印的文件目錄,第二個參數(shù)為打印后的文件名;
該腳本名稱為out_file_full_path
#!/bin/perluse 5.010;
my $dir = $ARGV[0]; # 此為第一個參數(shù);
opendir my $dh, $dir or die "Cannot open $dir: $!";my $out_file = $ARGV[1]; # 此為第二個參數(shù);
open OUT,">", $out_file or die "Cannot open $out_file:$!";foreach $file (readdir $dh) {next if $file eq '.' or $file eq '..'; # 將當前目錄.和上層目錄..排除在打印列表之外my $full_dir = "$dir/$file"; #加上目錄路徑,若不需打印目錄,則注釋改行,修改下一行的$full_dir為$file即可print OUT $full_dir . "\n"; #將文件和目錄寫入OUT文件中,每行添加一個"\n",用于換行print "One file in $dir is $file\n"; #此行為debug 調(diào)試打印,可注釋
}closedir $dh; # 關閉打開文件夾的句柄
close OUT; #關閉打開文件的句柄
舉例說明
# 現(xiàn)在在一個名為test的文件夾,test里面有3個文件,分別為test1.v,test2.v,test3.v
# 在終端中輸入上述命令
[xxx@local]$ ./out_file_full_path test test.out# 返回結果有兩個,第一個即為打印在終端的:
One file in test is test1.v
One file in test is test2.v
One file in test is test3.v
# 第二個為,輸出的文件,名稱為test.out
# 在終端中使用cat命令獲取文件內(nèi)容
cat test.out
# 返回值為
$PATH/test/test1.v
$PATH/test/test2.v
$PATH/test/test3.v
# 其中$PATH為test所在路徑