2017-10-28 13 views
0

私は基本的にはディレクトリに渡されたプログラムを持っています。ファイル名がパターンと一致する場合は、ディレクトリを作成し、その特定のファイルを移動します。ディレクトリ。一致しない場合は、PassedInDir/misc/ディレクトリに移動してください。Perlディレクトリがif(!-d)文を過ぎていますか? [readdir results]

どちらの場合も、私のプログラムはif(! -d $fp)のようなものです(私のプログラムはまだ対処できません)。

ディレクトリ内で初めて実行するとすべてうまく動作します。しかし、同じディレクトリ(ディレクトリのみを含むはずです)で再度実行すると、エラーCould not move file assignmentZ to destination DataB/misc at projectSorter.pl line 16.が発生します。 AssignmentZはディレクトリですが、何とか2番目の場合は(!-d)を過ぎてしまいます。

#!/usr/bin/perl -w 
use File::Copy; 
if(@ARGV < 1){ 
    print "\nUsage: proj6.pl <directory>\n\n"; 
    exit; 
} 
die("\nDirectory $ARGV[0] does not exist\n\n") if(! -e $ARGV[0]); 
opendir(DIR, $ARGV[0]) or die("\nCould not open directory $ARGV[0]\n\n"); 
while(($fp = readdir(DIR))){ 
    if($fp =~ m/proj(.*)\./){ 
     (! -d "$ARGV[0]/assignment$1") && (mkdir "$ARGV[0]/assignment$1"); 
     move("$ARGV[0]/$fp" , "$ARGV[0]/assignment$1") or die("Could not move file $fp to destination $ARGV[0]/assignment$1"); 
    } 
    elsif(! -d $fp){ #gets past here!!! 
     (! -d "$ARGV[0]/misc") && (mkdir "$ARGV[0]/misc"); 
     move("$ARGV[0]/$fp" , "$ARGV[0]/misc") or die("Could not move file $fp to destination $ARGV[0]/misc"); 
    } 
} 

私のプログラムを1回実行することでこれを実行するのは、これが唯一のディレクトリです。なぜこれが起こっているのか不思議です。

答えて

3

$fpreaddirがスキャンされたディレクトリに相対的です。 chdirをスキャンディレクトリに追加するか、またはスキャンしたディレクトリ名の前に-d testを追加します。

関数を移動するには、引数として"$ARGV[0]/$fp"を使用します。

perldoc -f readdir

"opendir関数" で開かれたディレクトリの次のディレクトリエントリを返します のreaddir DIRHANDLE
。 [...]
「readdir」の戻り値をファイル検査する予定の場合は、問題のディレクトリの先頭に置くことをお勧めします。 そうでなければ、そこに「chdir」しなかったので、間違ったファイルをテストするのは でした。

0

いくつかの提案があります。

‣Perlで-wフラグを使用しないでください。モジュールによっては、警告をオフにするものがありますが、-wフラグはグローバルです。それを無視して警告を報告します。

‣この2行は、常に各スクリプトの先頭に置いてください。

use strict; 
use warnings; 

これらのコードでは、多くのエラーが発生します。詳細については、perldoc strictおよびperldoc warningsを参照してください。

‣opendir/readdir/closedirの代わりにglob()またはFind::Findを使用してください。

‣の代わりにFile::Pathmake_path()を使用してください。

ifステートメントを&&ではなく条件付き実行に使用します。

‣コードに空白行を挿入すると、読みやすくなります。

File::FindおよびFile::pathは、Perlとともにインストールされる標準モジュールです。標準モジュールのリストについては、perldoc perlmodlibを参照してください。

#!/usr/bin/perl 

# -------------------------------------- 
# pragmas 

use strict; 
use warnings; 

# -------------------------------------- 
# modules 
use File::Copy; 
use File::Path qw(make_path); 

# -------------------------------------- 
# main 

# make sure there is something to work on 
if(@ARGV < 1){ 
    print "\nUsage: proj6.pl <directory>\n\n"; 
    exit; 
} 

# arguments should be directories 
for my $src_dir (@ARGV){ 

    # validate the source directory 
    die("\n$src_dir does not exist\n\n")  if(! -e $src_dir); 
    die("\n$src_dir is not a directory\n\n") if(! -d $src_dir); 

    # move proj* files 
    for my $proj (glob("$src_dir/proj*")){ 

     # get the proj number 
     (my $number) = $proj =~ m/proj(.*)\./; 

     # get the destination directory 
     my $dst_dir = "$src_dir/assignment$number"; 

     # create the directory where it goes 
     if(! -d $dst_dir){ 
      make_path($dst_dir) or die "could not make path $dst_dir"; 
     } 

     # move the file 
     move($proj, $dst_dir) or die("could not move file $proj to destination $dst_dir"); 

    } # end of $proj files 

    # move other files 
    for my $file (grep { ! -d } glob("$src_dir/*")){ 

     # get the destination directory 
     my $dst_dir = "$src_dir/misc"; 

     # create the directory where it goes 
     if(! -d $dst_dir){ 
      make_path($dst_dir) or die "could not make path $dst_dir"; 
     } 

     # move the file 
     move($file, $dst_dir) or die("could not move file $file to destination $dst_dir"); 

    } # end other files 

} # end of src_dir 
関連する問題