in reply to File renaming and removal to a subdirectory

Here is a quick thought (albeit untested):
my $indir = "/tmp/a"; my $outdir = "/tmp/b"; my @suffix = qw(A B C D E F ); ## ON AND ON foreach my $sfilename (@infiles) { $ofilename = "$outdir/$sfilename"; if (-e $ofilename) { ## it exists, lets add a suffix for (@suffix) { if (! -e "$ofilename.$_") { rename "$indir/$sfilename","$ofilename.$_"; print "Renamed to $ofilename.$_"; exit(0); } else { ## suffix alreay taken, try next print "$ofilename.$_ exists ...\n"; } } } else { ## does not exist, rename rename "$indir/$sfilename", "$ofilename"; } }

may the foo be with you

Replies are listed 'Best First'.
Re: Re: File remaing and removal to a subdirectory
by bwana147 (Pilgrim) on Jul 11, 2001 at 17:30 UTC
    albeit untested

    Indeed ;-) A couple of things go wrong. @infiles is not initialised, but I guess this is left as an exercise to the OP. No, I really wanted to point out the use of exit, which is not what you want here, IIUC. exit terminates the program however deep you are in nested blocs. By using exit here, you stop the program once the first file has been renamed with a suffix. Instead of that, you want to proceed to the next file.

    But next goes to the next iteration of the innermost loop. In our case, that would be the loop on suffices, not on files. So you'll need to use LABELS:
    FILE: foreach my $filename (@infiles) { ... if ( -e $ofilename ) { SUFFIX: for (@suffix) { if ( ! -e "$ofilename.$_" ) { ... next FILE; } else { ... } } } else { ... } }

    --bwana147