Title: RE: Copying Sub-Directories

You can use slightly ugly recursive code.. I use the following to delete files from the temp directories recursively, but it could be easily modified to copy them:

#!perl -s
use Cwd;

if (! &AlreadyExist("$ENV{TMP}")) {
        push(@TempDirs,"$ENV{TMP}");
}
if (! &AlreadyExist("$ENV{TEMP}")) {
        push(@TempDirs,"$ENV{TEMP}");
}
if (! &AlreadyExist("$ENV{WINDIR}\\TEMP")) {
        push(@TempDirs,"$ENV{WINDIR}\\TEMP");
}
if (! &AlreadyExist("$ENV{SystemRoot}\\TEMP")) {
        push(@TempDirs,"$ENV{SystemRoot}\\TEMP");
}
if (! &AlreadyExist("$ENV{SystemDrive}\\TEMP")) {
        push(@TempDirs,"$ENV{SystemDrive}\\TEMP");
}
print "Found these temporary directories on your system:\n";
print join "\n",@TempDirs;
print "\n\nPress <ENTER> to continue the script\n";
print "or Press <CTRL>-C to exit the script immediately: ";
$garbage = <STDIN>;

foreach $Dir (@TempDirs) {
        if (-e $Dir and -d $Dir) {
                print "\n-> Looking in directory $Dir\n";
                &ScanDir("$Dir");
        } # end of if
} # end of foreach

print "\n\nPress <ENTER> to exit the script: ";
$garbage = <STDIN>;
exit;

sub AlreadyExist {
        my $CurrentDir = $_[0];
        $equal = 0;
        foreach $Dir (@TempDirs) {
                if ($Dir eq $CurrentDir) {
                        $equal = 1;
                } # end of if
        } # end of foreach
        return $equal;
} # end of sub AlreadyExist

sub ScanDir {
        my ($workdir) = shift;
        my ($startdir) = &cwd; # keep track of where we began

        chdir($workdir) or die "\nUnable to enter dir $workdir:$!\n";
        opendir(DIR, ".") or die "\nUnable to open $workdir:$!\n";
        my @names = readdir(DIR) or die "\nUnable to read $workdir:$!\n";
        closedir(DIR);

                foreach my $name (@names) {
                next if ($name eq ".");
                next if ($name eq "..");
                if (-d $name) { # is this a directory?
                        print "\n-> Looking in directory $name";
                        &ScanDir($name);
                        print "\nAttemtping to delete directory $name";
                        rmdir($name) or warn "\nUnable to remove directory $name: $!\n";
                        next;
                } # end of if
                if ($name ne "") {
                        print "\nAttemtping to delete file $name";
                        unlink($name) or warn "\nUnable to delete file $name: $!\n";
                } # end of if
        } # end of foreach
        chdir($startdir) or die "\nUnable to change to dir $startdir: $!\n";
} # end of sub ScanDir

-----Original Message-----
From: rAuL [mailto:[EMAIL PROTECTED]]
Sent: Monday, April 01, 2002 01:18 PM
To: Edgington, Jeffrey
Cc: rAuL; [EMAIL PROTECTED]
Subject: RE: Copying Sub-Directories


This would not do becuase I once had a situation where the verswion of XCOPY on Windows 2000 was different from the one with NT 4.0.

On 01 Apr 2002, Edgington, Jeffrey wrote:

> I would use xcopy though.
>
> system ("xcopy <source> <dest> \/E \/-Y");
>
> /E - copies dirs and subdirs including
> empty ones.
>
> /-Y -no prompting when overwriting
>
> Just do 'xcopy /?' at a cmd to look at
> additional options for copy.
>
> jeff e.
>
>
>
> -----Original Message----- From: rAuL
> [mailto:[EMAIL PROTECTED]] Sent: Monday,
> April 01, 2002 1:02 PM To:
> [EMAIL PROTECTED]
> Subject: Copying Sub-Directories
>
>
>
> Much to my surprise and inexperience, I cannot
> find a Perl command to do wilcard copying of
> files and subdirectoies within a directory. Is
> there such a thing?
>
> Thanks
> _____________________________________________-
> __
> Perl-Win32-Admin mailing list
> [EMAIL PROTECTED] To
> unsubscribe: http://listserv.ActiveState.com/-
> mailman/mysubs
_______________________________________________
Perl-Win32-Admin mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to