in reply to Open a folder
Dear Monk,
First, excuse the professor in me, let me make a minor correction to the terminology you are using. What you refer to as a "folder" is actually a directory and Perl provides a wonderful function for reading directories. Here is some sample code to help you along your way:
the -d $entry is to prevent you from attempting to do something unintended with a subdirectory under the desired directory. After you get past all the next logic (which could have been combined in one statement) you then add your code to actually do something with the file names you would get as a result of the rest of the code.#!/usr/bin/perl -w use strict; my $dirname="/path/to/the/directory"; opendir(DIR,$dirname) or die "$dirname:$!"; while (my $entry=readdir(DIR)){ next if $entry eq '.'; next if $entry eq '..'; next if -d $entry; # see notes below | do something with this... } exit(0);
If you want to recursively operate on that directory here is an example of that code well modified:
Clear as mud?#!/usr/bin/perl -w use strict; workTheDirectory("/path/to/the/directory"); exit(0); # # sub workTheDirectory { my $dirname=shift; opendir(DIR,$dirname) or die "$dirname:$!"; while (my $entry=readdir(DIR)){ next if $entry eq '.'; next if $entry eq '..'; if ( -d $entry ) { my $newdir = $dirname . "/" . $entry; #grow the path workTheDirectory($newdir); } | This is a file... | work it. } return; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Open a folder
by Dr Manhattan (Beadle) on Jan 08, 2013 at 14:35 UTC | |
by ww (Archbishop) on Jan 08, 2013 at 14:53 UTC | |
by blue_cowdawg (Monsignor) on Jan 08, 2013 at 14:51 UTC | |
by Dr Manhattan (Beadle) on Jan 09, 2013 at 06:15 UTC | |
by blue_cowdawg (Monsignor) on Jan 09, 2013 at 14:04 UTC | |
|
Re^2: Open a folder
by BillKSmith (Monsignor) on Jan 08, 2013 at 14:42 UTC | |
by ww (Archbishop) on Jan 08, 2013 at 14:56 UTC | |
by blue_cowdawg (Monsignor) on Jan 08, 2013 at 14:48 UTC |