Avatar of raaj4354
raaj4354
 asked on

Write filenames in a directory recursively to text file

Have a directory structure DIR1/
                               FOLDER1/fileabc.sps
                                                     CVS/abc.txt
                                                              def.entries
                                                                  cfe.prop
                                             FOLDER2/filebcd.sql
                                                     CVS/abc.txt
                                                              def.entries
                                                                  cfe.prop  
                                             FOLDER3/file1def.sp
                                                     CVS/abc.txt
                                                              def.entries
                                                                  cfe.prop
                                             FOLDER4/file1cvf.ty
                                                    CVS/abc.txt
                                                              def.entries
                                                                  cfe.prop
                                          
Recursively write all the filenames without extensions in to a text file
Need to ignore CVS folder and files in them... in each subdirectory.
Ignore a testfile.txt in each of the folder
Take path/to/DIR1 as parameter to the script
PerlScripting Languages

Avatar of undefined
Last Comment
raaj4354

8/22/2022 - Mon
wilcoxon

I would actually do that from the command line...

find DIR1 -type d -name CVS -prune -o -type f -print | egrep -v 'testfile\.txt$' |sed 's%\.[a-z]*$%%' > output_file

Open in new window


If you really want to use perl then this should do it...

#! /usr/bin/perl
use strict;
use warnings;
use File::Find ();

my $dir = shift or die "Usage: $0 path_to_base_dir\n";

# for the convenience of &wanted calls, including -eval statements:
use vars qw/*name *dir *prune/;
*name   = *File::Find::name;
*dir    = *File::Find::dir;
*prune  = *File::Find::prune;

sub wanted;

# Traverse desired filesystems
File::Find::find({wanted => \&wanted}, $dir);
exit;

sub wanted {
    my ($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_);
    if (-d _ and /^CVS\z/s) {
        $File::Find::prune = 1;
        return;
    }
    if (-f _ and !/^testfile\.txt\z/s) {
        s{\.[a-z]+$}{};
        print OUT $_, "\n";
    }
}

Open in new window

wilcoxon

Just reread your post and have one question - by "Recursively write all the filenames without extensions in to a text file" do you mean:
1) write the list of files that don't have an extension
2) write the list of base filenames (extensions stripped)
3) something else
wilcoxon

Just noticed an omission in my code - you need to add the below line before line 17:

open OUT, ">output_file.txt" or die "could not write output: $!";
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
raaj4354

ASKER
I meant to  write the list of base filenames (extensions stripped).

Everything works perfect... but i need to exclude a text file which is present in each subfolder. (test_text.txt). The list is showing this file name too.
ASKER CERTIFIED SOLUTION
wilcoxon

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
raaj4354

ASKER
Great and fast response.. Thanks a lot..!!
raaj4354

ASKER
How can i pass the directory as a parameter in which the output.txt file is stored..

I want to put the output file in a directory which i want to specify while running the command...
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.