First of all, don't worry about asking questions that you think are stupid.
Your code has a number of problems:
- The first open call will, AFAIK, always fail because you are chdir()ed to the directory that contains the directory you've just found. So you're trying to write to a file with the same name as a directory. See the section about "the wanted() function" in the File::Find manpage.
- I don't thint the space in "> $dir" is a good idea - do ">$dir" instead.
- What is $file? You only use it once as far as I can see. This is where use warnings; can help you.
Anyway, none of this really helps you solve your problem. I would put the directory listings in a separate directory. Take a look at this code (untested):
use strict;
use warnings;
use File::Find;
my $listings_dir = 'c:/listings';
find(\&callback, 'c:/tmp');
sub callback {
my $file = $_;
# Check to see if $file is a directory
if (-d $file) {
# Get a list of all the files in the directory (inefficient)
# (If you only want the filenames rather than paths as well,
# investigate using File::Basename. If you want just the files
# in a directory, and directories within the directory,
# consider putting grep { -f $_ } before the glob.
my (@files) = glob($File::Find::dir . "/$_/*");
# Save this list to a file
open LISTING, ">$listings_dir/list$_.txt";
print LISTING join ("\n", @files);
close LISTING;
}
}
Good luck with learning Perl. I strongly recommend learning Perl properly--get yourself a copy of "Learning Perl". Your investment in learning Perl will pay back very quickly.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.