Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Open a folder

by blue_cowdawg (Prior)
on Jan 08, 2013 at 14:04 UTC ( #1012235=note: print w/ replies, xml ) Need Help??


in reply to Open a folder

      but I am struggling to open a folder using Perl.

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:

#!/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);
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.

If you want to recursively operate on that directory here is an example of that code well modified:

#!/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; }
Clear as mud?


Peter L. Berghold -- Unix Professional
Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg


Comment on Re: Open a folder
Select or Download Code
Re^2: Open a folder
by Dr Manhattan (Beadle) on Jan 08, 2013 at 14:35 UTC

    Hi, thank you for the help. I am not completely sure what your code is does, but the

    next if $entry eq '.'; next if $entry eq '..';

    did help. I have combined your code with something of another user to come up with this:

    #!/usr/bin/perl -w use strict; use warnings; open (Output, ">Output.txt") or die "Can't open"; my $tmp_dir= 'C:\Users\ZB\Desktop\Text Files'; opendir (DIR, $tmp_dir) or die $!; while (my $file_name = readdir(DIR)) { next if $file_name eq '.'; next if $file_name eq '..'; print "$file_name\n"; } close (Output) closedir(DIR);

    All it does is print all the file names in my directory as output(so at least I am on the right path). However I want to iterate through each text file and do some basic calculations(all in 1 sub) within each file. Any ideas?

          However I want to iterate through each text file and do some basic calculations(all in 1 sub) within each file.

      Add the sub to your code thusly:

      | in your while loop invoke: process_file($tmp_dir,$file); | and then after your loop sub process_file { my($path,$node)=@_; my $fname = $path . '\' . $node; open FIN,"<$fname" or die "$fname:$!"; while (my $line=<FIN>){ chomp $line; | do something here. } }


      Peter L. Berghold -- Unix Professional
      Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

        Hi blue cowdawg

        I tried this:

        #!/usr/bin/perl -w use strict; use warnings; my $dir = 'C:\Users\ZB\Desktop\Text Files'; opendir (DIR, $dir) or die $!; while (my $file = readdir(DIR)) { next if $file eq '.'; next if $file eq '..'; &countWords($dir, $file); #call sub } sub countWords { my $line = @_; #receive file as input my @array = split(/ /, $line); print "$#array\n"; } closedir(DIR);

        It is the same thing as earlier, I just added a small sub which is supposed to parse each file into an array, count the words and print the output(total words). Did I call the sub correctly? And does the sub receive the file correctly? When I run the script it gives the same answer(total words) for each text file, which is incorrect.

        Thank you

      see perldoc -f push and Tutorials (below the text explaining how to/when to post a new tut) or Super Search re iterating through arrays.

      IOW, collect all the relevant $file_name entries into an array; at that point, iterating thru the individual files requires only that you attempt to inform yourself thru study of the references above.

      Update: Of course (/me says belatedly), you'll need to consider executing your script from an appropriate directory or troubling yourself to learn about perldoc -f chdir, perldoc cwd, etc.

Re^2: Open a folder
by BillKSmith (Chaplain) on Jan 08, 2013 at 14:42 UTC
    The advantage of your "correction" is clear. However, note that "folder" is the correct terminology for that structure in windows. The OP has consistently explained the problem from an application view.
    Bill
          "folder" is the correct terminology for that structure in windows.

      The term "folder" is correct for any GUI environment that represents directories as such. At the system level where I live it's a directory.


      Peter L. Berghold -- Unix Professional
      Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
      BillKSmith
      Correct terminology, perhaps, if you buy (hook, line and sinker) M$'s (non-standard) terminology as an acceptable standard.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1012235]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (16)
As of 2014-08-29 16:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (282 votes), past polls