Roboticus
Using $File::Find::name rather has undesired effects.
#! /usr/ddsperl/bin/perl -w
# Testing
# Author:
#use strict;
use diagnostics;
use File::Find;
# Variable "Error" contains any error of previous task runs.
# Variable "ThisMoveTarget" contains destination archive location path
+. If this stores a path (i.e if directories are archived) then
# the main body of code (i.e walk down the source location for deletin
+g empty directories) should execute
my $Error = "0" ;
# my $ThisMoveTarget = q|holds location where archive files should go
+|;
my $ThisMoveTarget = '/home/ssaha/tmp/archive';
# our $DelEmptyFoldrArchive = q|holds yes or no |;
our $DelEmptyFoldrArchive = "yes ";
# our $DelEmptyFoldrArchive = "no ";
# our $ThisSourceDirectory = q|holds the source file location |;
our $ThisSourceDirectory = "/home/ssaha/tmp/source/";
#our $ThisSourceDirectory = "\\hostname\share-dir";
if ($ThisSourceDirectory =~ m/^\\\\/ ) {
($ThisSourceDirectory =~ s/[\\]/\\\\/g ) ;
}
my $file ;
if (($DelEmptyFoldrArchive eq "yes ")&&($ThisMoveTarget)) {
if (($ThisSourceDirectory)&&("$Error" == 0)) {
find({ wanted => \&wanted, no_chdir => 1 }, $ThisSourceDirectory);
sub wanted {
return unless -f;
$file = $_;
print STDERR "$file \n " ;
}
if ($file) {
print STDERR "The $ThisSourceDirectory has files pres
+ent \n" ;
}
elsif (!$file) {
finddepth (sub {
print " directories are $File::Find::name \n";
rmdir $File::Find::name ;
# rmdir $_ ;
# rmdir
}, $ThisSourceDirectory);
}
}
elsif (($ThisSourceDirectory)&&("$Error" != 0)) {
print STDERR "JOB Completed with Errors, Source Agent used was SRCHOST
+\n" ;
}
}
else {
print STDERR "DelEmptyFoldrArchive is $DelEmptyFoldrArchive,
+ skipping directory deletion. \n" ;
}
When used File::Find::name with rmdir , it deletes the source directory itself ($ThisSourceDirectory=/home/ssaha/tmp/source
) upon condition being satisfied (empty directory) and not the constituent sub directories inside. The only two options i am finding safe is "rmdir $_" and "rmdir". Even though they are working as a code test on separate linux environ --on actual test script(code) is only able to delete on windows clients locally (c:\path etc format. Its not able to delete inputs such "\\hostname\share\" not even in Linux ("\usr\local\tmp") type. I think there a possibility that this is because of rmdir not getting full pathname and i am not getting how it could be achieved safely.
output
[scripts]# perl ./testArchive.pl
directories are /home/ssaha/tmp/source/dir1/folder
directories are /home/ssaha/tmp/source/dir1
directories are /home/ssaha/tmp/source/dir3/folder
directories are /home/ssaha/tmp/source/dir3
directories are /home/ssaha/tmp/source/dir2/folder
directories are /home/ssaha/tmp/source/dir2
directories are /home/ssaha/tmp/source
[root@hostname scripts]# ls -l /home/ssaha/tmp/source
ls: /home/ssaha/tmp/source: No such file or directory
[root@nuuuniitsahalab scripts]# ls -l /home/ssaha/tmp/
total 0