smturner1 has asked for the wisdom of the Perl Monks concerning the following question:

If you go down 3/4 to the comments it's a file and it's a directory, you will see where I am trying to evaluate if the variable is a file or a directory. It is not evaluating a word doc or a .dll file as a file and directories with word docs in them will not process as a directory. Any help and/or advise would be much appreciated.

use strict; use warnings; use diagnostics; use autodie; use File::Copy::Recursive qw( rcopy ); #Give +s you access to the "move" command use File::Path qw( make_path remove_tree ); #Copies data recursively f +rom one dir and creates a new dir to paste files to. use File::Glob qw( :globally ); use File::Find; use File::Spec; use File::Basename; use Getopt::Long; # support options+arguments use Pod::Usage; # avoid redundant &Usage() use POSIX qw(strftime); use File::Spec::Functions; #---------------------------------------------# # Check command line args. # #---------------------------------------------# my ( $opt_debug, $opt_help, $opt_man, ); $opt_debug = 0; GetOptions( 'debug!' => \$opt_debug, 'help!' => \$opt_help, 'man!' => \$opt_man, ) or pod2usage(-verbose => 1) && exit; pod2usage(-verbose => 1) && exit if defined $opt_help; pod2usage(-verbose => 2) && exit if defined $opt_man; # Make sure a file name is given ... if ( 3 != @ARGV) { warn "Invalid syntax\n"; pod2usage(-verbose => 1) && exit -1; } #----------------------------------------------# #Set command line arguments # #----------------------------------------------# my ($website, $old_ip, $new_ip) = @ARGV; #Set vars my $TIMESTAMP = strftime('%Y%m%d', localtime); my $volume = 'C:/Users/Shaun/'; my $SourceDir = File::Spec->catpath($volume, qw( Desktop Source_d +ir )); my $destinationDir = File::Spec->catpath($volume, qw( Desktop ), $webs +ite); my $ARCHIVE = File::Spec->catpath($volume, qw( Desktop ), join( +'.', $website, $old_ip, $TIMESTAMP)); my $file; my ( @kept, @EXCLUSIONS, @errors, @file_list, ); my @WEBSITES = qw( three five calnet_test ); my @dfiles = glob( "$destinationDir/*" ); #This sets the array variabl +e for subroutine process_dir my $remaining = process_dir($ARGV[0]); #This sets the variable for +the selective_delete_err_handle subroutine #Create Archive Directory make_path ( $ARCHIVE ) or die "Archive Folder was not created $!"; #Set procedure to do full backup sub full_backup { # "rcopy" is recursive. rcopy( $destinationDir, $ARCHIVE ) or die $!; return; } # error handling for selective delete sub selective_delete_err_handle { if ( $ARGV[0] =~ m{ (?: three | five | calnet_test ) }ixms ) { die "Oops! you're trying to whack $ARGV[0]\n"; } #If $remaining and @EXCLUSIONS have an equal numeric value, no error. if ($remaining != @EXCLUSIONS ) { print STDERR "Yikes! not all exclusions were found, or some were d +eleted: "; } printf STDERR "$remaining remaining %d exclusions\n", scalar @EXCL +USIONS; #Creates a list of files that were not removed from the directory and +prints each dir/file. if (@kept) { printf STDERR "Kept:\n %s\n", join "\n ", @kept; } } sub process_dir { my @EXCLUDES = ( 'Support', 'Testimonies/*', 'Sermons/*', "$website", ); my $dir = shift; print STDERR "Processing directory $dir ...\n"; return 1 if ! -d $dir; opendir(my $website, $dir) || die "can't opendir $dir: $!"; my @files = grep { ! /^\./ } readdir($website); closedir $website; my @remaining = grep { -f "$dir/$_" } @files; my $remaining = @remaining; printf STDERR "%d files found, $remaining remaining\n", scalar @fi +les; FILE: for my $file (@dfiles) { # It's a directory ... if ( -d "$dir/$file" ) { my $undeleted += process_dir("$dir/$file"); print STDERR "rmdir $dir/$file\n" if $undeleted == 0; for my $excl ( @EXCLUDES ) { if ( $excl eq basename $file ) { print STDERR "Excluded: $file\n"; push @kept, $file; next FILE; } } remove_tree "$dir/$file"; $remaining += $undeleted; } # It's a file ... elsif ( -f "$dir/$file" ) { print STDERR "File: $file, $remaining remaining\n"; # Don't delete files in the exclusion list. for my $excl ( @EXCLUDES ) { if ( $excl eq basename $file ) { print STDERR "Excluded: $file\n"; push @kept, $file; next FILE; } } print STDERR "unlink $dir/$file\n"; unlink "$dir/$file"; # This should go to 0 if no excluded files were found. $remaining--; # } else { die "Not a directory and not a file: $file"; } } return $remaining; } full_backup; selective_delete_err_handle; process_dir;

Replies are listed 'Best First'.
Re: Word docs not setting if(-f "$var") to true
by ikegami (Patriarch) on Jul 02, 2014 at 04:07 UTC

    You weren't clear on what happens. You said what didn't happen, but left a couple of possibilities as to what did happen. Clearer information would be appreciated, but first change your code to the following to get more information to diagnose the problem. Provide the output you get for files/dirs that give undesired behaviour.

    my @stat = stat("$dir/$file") or die("Can't stat \"$dir/$file\": $!"); if ( -d _) { warn("\"$dir/$file\" is a dir\n"); ... } elsif (-f _) { warn("\"$dir/$file\" is a file\n"); ... } else { die(sprintf("\"%s\" is neither a file nor a dir (mode=%o)\n", "$dir/$file", $stat[2], )); }
Re: Word docs not setting if(-f "$var") to true
by GrandFather (Saint) on Jul 02, 2014 at 02:13 UTC

    Maybe the issue is that @dfiles entries already have a directory path? Use the debugging facility in your IDE to stop on the if statement and examine the contents of interesting variables, or use Perl's command line debugging or scatter diagnostic print or warn statements through your code so you can see what's going on.

    Trimming your code down to the three lines needed to test just the immediate issue will help too!

    Perl is the programming world's equivalent of English