Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to ingore directories/files when using File:find

Status
Not open for further replies.
Jun 3, 2007
84
0
0
US
Hello I was wondering what the best way to go about ignoring directories/files that are owned by the OS/system when using File:find:name

The problem that I am running into is that there are two/three directories/files that file:find:name can't get into or that when opening a filehandle fails b/c of permissions one of them being System Volume Information. So when file:find:name traverses to that directory the script fails and states permission denied.


I am using the code below along with other code, but the problem that I am encountering is like I mentioned running into files/directories which can't be read/opened mainly System Volume Information and pagefile.sys. I've tried adding the following but with no luck.

return if -r "$found_files";

If I run the script against a directory like my desktop or tmp directory it works just fine, just running into system files which i want to skip or ignore when they can't either be traversed or opened.

Thanks for help

Code:
sub edits() {
    if ( -f ) {
        foreach ( $File::Find::name ) {
            open(LOG, "< $File::Find::name") or die "Could not open file $_: $!";
            while ( <LOG> ) {
                my $LINE = $_;
                if ( $LINE =~ m/$string/ ) {
                    print "Looking for: $string\n Found it in file: $LINE\n";
                    if ( $LINE =~ m/$updated_string/ ) {
                        print "Looking for: $updated_string\n Found it in file: $LINE\n";
                    }
                }
            }
        }
    }
    else {
        print "Could not find the second string: $updated_string\n";
}
 
You could try something like this (untested):
Code:
sub edits() {
    if ( -f ) {
        foreach ( $File::Find::name ) {
            eval {
                open(LOG, "< $File::Find::name") or die "Could not open file $_: $!";
            };
            if ($@) {
                warn $@;
                next;
            }
            while ( <LOG> ) {
                my $LINE = $_;
                if ( $LINE =~ m/$string/ ) {
                    print "Looking for: $string\n Found it in file: $LINE\n";
                    if ( $LINE =~ m/$updated_string/ ) {
                        print "Looking for: $updated_string\n Found it in file: $LINE\n";
                    }
                }
            }
        }
    }
    else {
        print "Could not find the second string: $updated_string\n";
}
 
Code:
open(LOG, "<$File::Find::name") or return(0);


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top