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
Avatar of wilcoxon
wilcoxon
Flag of United States of America image

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

Avatar of wilcoxon
wilcoxon
Flag of United States of America image

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
Avatar of wilcoxon
wilcoxon
Flag of United States of America image

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: $!";
Avatar of raaj4354
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
Avatar of wilcoxon
wilcoxon
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of raaj4354
raaj4354

ASKER

Great and fast response.. Thanks a lot..!!
Avatar of raaj4354
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...
Scripting Languages
Scripting Languages

A scripting language is a programming language that supports scripts, programs written for a special run-time environment that automate the execution of tasks that could alternatively be executed one-by-one by a human operator. Scripting languages are often interpreted (rather than compiled). Primitives are usually the elementary tasks or API calls, and the language allows them to be combined into more complex programs. Environments that can be automated through scripting include software applications, web pages within a web browser, the shells of operating systems (OS), embedded systems, as well as numerous games. A scripting language can be viewed as a domain-specific language for a particular environment; in the case of scripting an application, this is also known as an extension language.

30K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo