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

Hello, Im having trouble getting my C directory files and directories on seperate terms, from there I want to write the files from C into one text file for reference, and all the directories from C into another text file. this is what I have so far, it prints files but not the files from C, infact im not sure where theyr coming from :)Thank you.

opendir C, "C:\\"; @C_DIR = (readdir(C)); closedir C; open (Ctxt, ">full system memory\\C_DRIVE.txt"); foreach $C_FILE (@C_DIR) { if (-f "C:/$C_FILE") { print Ctxt "$C_FILE"; print Ctxt"\n"; } } close (Ctxt);

Replies are listed 'Best First'.
Re: storing all file info
by bobr (Monk) on Dec 31, 2009 at 15:36 UTC
    Hi, I am not sure I understand properly, but this should create two files, one with list of files, another with list of directories. I tried to keep your original code as intact as possible.
    opendir C, "C:\\"; @C_DIR = (readdir(C)); closedir C; open(Ctxt_files, ">full system memory\\C_DRIVE_FILES.txt"); open(Ctxt_dirs, ">full system memory\\C_DRIVE_DIRS.txt"); foreach $C_FILE (@C_DIR) { if (-f "C:/$C_FILE") { print Ctxt_files "$C_FILE\n"; } elsif( -d "C:/$C_FILE") { print Ctxt_dirs "$C_FILE\n"; } } close(Ctxt_files); close(Ctxt_dirs);

    Note it also lists hidden files.

    -- hope this helps, Roman

      Better, also, to check your opens.
      See perldoc -f die or Super Search for nodes where "open" and "test" both occur.

      thanks roman, and I apreciate that you kept my original code, it works great ... my problem was simply not knowing I would get hidden files I mistaken them for being in the rong directory :) thank you and happy newyears.

Re: storing all file info
by ~~David~~ (Hermit) on Dec 31, 2009 at 15:33 UTC
    How about something like this:
    use strict; use warnings; use File::Slurp; my $dir = 'C:/'; my $dir_file = 'C:/directories.txt'; my $file_file = 'C:/files.txt'; my @files_and_dirs = read_dir( $dir ); foreach my $file ( @files_and_dirs ){ if ( -d $dir.'/'.$file ){ $file .= "\n"; append_file( $dir_file, $file ); } elsif ( -f $dir.'/'.$file ){ $file .= "\n"; append_file( $file_file, $file ); } }