I'm very new to perl but have picked up the (very) basics over the last few days. I've been given a task by my work to duplicate a directory structure from one location to another.
The guy thats given me the challenge wants me to use a recursive subroutin.
Ok, so the dir structure I'm using is like this.
All this in C:\test
Folder A/Folder 1
Folder A/Folder 2
Folder A/Folder 2/Folder 3
Folder A/Folder 2/Folder 4
Folder B
Folder B/Folder 5
Folder B/Folder 6
Folder B/Folder 6/Folder 7
Folder B/Folder 6/Folder 8
I need to copy this directory structure (no files) to C:\new
I can get my subroutine to be recursive, but I can't get it to come out of a tree path.
This is my code so far
I can't get the code to recurse properly to come back out of the path its reached the end of, to start on the next tree down.
I hope someone understands what I mean, and can point me in the direction of where I'm going wrong.
Thanks in advance.
Ant
The guy thats given me the challenge wants me to use a recursive subroutin.
Ok, so the dir structure I'm using is like this.
All this in C:\test
Folder A/Folder 1
Folder A/Folder 2
Folder A/Folder 2/Folder 3
Folder A/Folder 2/Folder 4
Folder B
Folder B/Folder 5
Folder B/Folder 6
Folder B/Folder 6/Folder 7
Folder B/Folder 6/Folder 8
I need to copy this directory structure (no files) to C:\new
I can get my subroutine to be recursive, but I can't get it to come out of a tree path.
This is my code so far
Code:
$startDir = 'c:\test';
$newDir = 'c:\new';
sub myReadDir {
my ($dir) = @_;
opendir (DIR, $dir);
@dir = readdir (DIR);
closedir(DIR);
foreach my $file (@dir) {
if (!-f $file && $file !~ /\.+/) {
print $newDir . "\\" . $file . "\n";
$tempDir = $dir;
$nextDir = $dir . "\\" . $file;
$newDir = $newDir . "\\" . $file;
myReadDir ($nextDir);
}
}
}
myReadDir($startDir);
I can't get the code to recurse properly to come back out of the path its reached the end of, to start on the next tree down.
I hope someone understands what I mean, and can point me in the direction of where I'm going wrong.
Thanks in advance.
Ant