tevolo has asked for the wisdom of the Perl Monks concerning the following question:
This is what I am trying to do which I thought would be straight forward but is causing me problems.
I am starting with a base directory that has many subdirectories under it, and those directories also have subdirectories, and I am trying to write to a file a list of all the subdirectories of any particular directory and an empty file if the directory has no subdirectories.
For instance: /about/ /about/me/ /about/you/ /about/me/myself/ would write a file in /about/ with me you a file in /you/ that is empty a file in /me/ with myself
I have tried a number of things but none are working so I am coming here for help
Thanks again for any help
Hello, I got it working. I was not returning to the find directory and that was causing the problem. Thanks for all the comments.
Here is the code, bad as it may be, for anyone else who might wish to do something similar.
#!c:/Perl/perl/bin/perl.exe use strict; use warnings; use FileHandle; use File::Find qw(find); use File::Basename; use File::Copy; use File::Find; use Cwd; my $BaseDir = 'C:/Temp3/jcr_root/content/acs/'; find( \&createContentxml, $BaseDir ); sub createContentxml { if (-d $File::Find::name) { my $dir = $File::Find::name; # print $dir . ": \n"; &subdir($dir) ; } } sub subdir { my $dir2 = getcwd; print $dir2 . "\n"; my $dir = $_[0]; chdir($dir) or warn " not able to cd into $dir \n"; print "dir: $dir: \n"; opendir DH, $dir or die "Failed to open $dir: $!"; while ($_ = readdir(DH)) { next if $_ eq "." or $_ eq ".."; if (-d $_ ) { print $_ . "\n"; open (MYFILE, '>>test7.txt'); print MYFILE $_ . "\n"; close (MYFILE); } else { next; } } open (MYFILE, '>>empty.txt'); print MYFILE " empty \n"; close (MYFILE); chdir($dir2); return 0; # Did not find any subdirectory in this directory }
|
|---|