Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

visit directories and more...

by star7 (Novice)
on May 25, 2003 at 13:51 UTC ( [id://260689]=perlquestion: print w/replies, xml ) Need Help??

star7 has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/bin/perl -w sub dir_explorer { my $dir = $_[0]; opendir(DIR,$dir) or die "Konnte Verzeichnis $dir nicht öffnen: $!"; my @verzeichnis = readdir(DIR); close(DIR); open(FILE,">output.dat"); foreach $entry (@verzeichnis) { if(-f $entry){ # formatieren($entry); print FILE "$entry ist ein file ! \n"; } else { print FILE "$entry ist ein directory ! \n"; dir_explorer($entry); } }; close(FILE); }; dir_explorer("/home/du/Stelle");
work on files in subdirectories --- How???

Hello, I have the following problem: I have a directory,
where subdirectories are, which contain text files. I wrote a Perl script(not this one), which formats
the text files. The script is to be extended (see the script at the top). Each
subdirectory is to be visited, be formatted gradually the text files therein
and attached to a text file, which has the name of the subdirectory at the end and contains all formatted text
files of the subdirectory. The skript doesn`t work. I need your help.
Can you help me ? I think, i have to use a solution with FILE::Find. But i am a beginner and i can`t find similar problems and their solutions in my books. Please help me if you can. Thanks.

Replies are listed 'Best First'.
Re: visit directories and more...
by little (Curate) on May 25, 2003 at 13:59 UTC
Re: visit directories and more...
by Abigail-II (Bishop) on May 25, 2003 at 14:04 UTC
    Ah, a "doesn't work" question. Not even the slightest indication what goes wrong. Does it sit all day on the couch watching soaps? Maybe you should pay it more. Please don't assume people here all have superpowers and can read your mind. Explain what you expect your program to do, and what it does, not "it doesn't work", because that doesn't carry any information (if it would work, would you post about it?).

    One thing that you might want to pay attention to is that if you read the content of a directory, and then want do something with the files and directories (like opening, or performing file tests on them), you should either prepend the name of the directory there where you need the name of the file as an argument, or your current working directory should be the same as the directory you just read.

    Abigail

File::Find::Rule
by barbie (Deacon) on May 26, 2003 at 09:23 UTC
    Can I suggest you have a look at File::Find::Rule. It makes picking up the exact files you want from your start directory, a lot easier. It returns an array of found files with the full path, so you can pass that straight to open and get the contents of only the files you want. It can get rid of several lines of checking for . .. and any other system type files.

    Excluding core modules, it's my most used module.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Barbie
    Birmingham Perl Mongers
    Web Site: http://birmingham.pm.org/
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Re: visit directories and more...
by star7 (Novice) on May 25, 2003 at 14:01 UTC
    Sorry, wrong script .. I mean this script:
    #!/usr/bin/perl -w sub dir_explorer { my $dir = $_[0]; opendir(DIR,$dir) or die "Konnte Verzeichnis $dir nicht öffnen: $!"; my @verzeichnis = readdir(DIR); close (DIR); foreach $entry (@verzeichnis) { if(-f $entry) { # formatieren($entry); print ("$entry ist ein file ! \n"); } else { next if $entry =~ /^.\.?$/; # . und .. überspringen print ("$entry ist ein directory ! \n"); dir_explorer($entry); } }; }; dir_explorer("/home/dub/Stelle");
      Your first request indicated that you wanted to add the information to a text file, and the code you posted originally had the steps to create and open a FILE. This version is missing that step. Are you still trying to write to a File with this version?

      Also, I wonder if you wouldn't want to skip the . and .. entries at the very beginning, and move
      next if $entry =~ /^.\.?$/;   # . und .. überspringen
      up to the beginning of your if block?
      WB

      @little,WhiteBird and others Thank you. I read the tutorial about FILE::Find and wrote a new script. But there is a new problem. I hope you can help me:
      #!/usr/bin/perl -w use File::Find; find(\&print_name_if_dir, "/home/dub/Stelle"); sub print_name_if_dir { my $file = $_; if (-d $file) { $dir=$file; open(FILE, "> $dir") or die "Konnte $dir nicht mit Schreibrecht +en öffnen: $!\n"; close(FILE); }; if (-f $file) { open(FILE, ">> $dir") or die "Konnte $dir nicht öffnen: $!\n"; print FILE "$file\n"; close(FILE); }; };
      1.if-command: open(FILE,"> $dir") - I want create a new file (text-file) which has the name of the visited directory.
      But $dir contains the directory. How can i solve this problem ?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://260689]
Approved by little
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-26 00:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found