in reply to How to get sub-folder names using perl?

use strict; use warnings; use File::Path qw( make_path ); use File::Slurp; use Cwd; my $cwd = getcwd(); my @dirs = read_dir($cwd); for(@dirs){ next unless -d $_; make_path( "$cwd/result/$_" ); open (my $file, '>', "$cwd/result/$_/test file for $_.txt") or die + "$!"; print $file "this is a test for directory $_"; #or print calculati +on to $file }
But i am having a little bit of a time trying to understand the question. This will only create folders in "result" directory if there are folders in "test" directory. The actual file handling wouldnt be much more trivial than this ;)

EDIT: Updated the code to reflect advice described below

Replies are listed 'Best First'.
Re^2: How to get sub-folder names using perl?
by erhan (Novice) on Mar 08, 2015 at 05:49 UTC

    @james28909, thanks. It works. But I didn't understand this line: next if -f $_;

    Can you please explain that line? Why did you use -f and $_? Thanks again.
      You said that you want to work on subfolders. The
      next if -f $_;
      checks if the current directory entry (contained in $_) is a regular file (as opposed to directories or folders in MS parlance), and, if it is, goes directly to the next directory entry in the list. This way, the script will only work on subdirectories.

      Update: as correctly pointed out below by Happy-the-monk, the code above will filter out only plain files (I used the words "regular files" to mean that). So that it is not completely accurate to write, as I did above, that the script will only work on subdirectories, since there can be other entities than just plain files and subdirectories. And it would be better to identify real directories with something like next unless -d $_;. Thank you, Happy-the-monk, for the added accuracy.

      Je suis Charlie.

        This way, the script will only work on subdirectories.

        it will only work on files that are not "plain files", but will still include unwanted entities like symlinks, named pipes, sockets, block and character files.

        Instead of next if -f $_; which is not that accurate, it is much better to use the already suggested next unless -d $_; which does the trick of doing no more and no worse of skipping everything that's not a directory.

        Cheers, Sören

        Créateur des bugs mobiles - let loose once, run everywhere.
        (hooked on the Perl Programming language)