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

This node falls below the community's minimum standard of quality and will not be displayed.
  • Comment on Identifing all filenames over 250 letters long

Replies are listed 'Best First'.
Re: Identifing all files over 250 letters long
by marto (Cardinal) on Jul 30, 2009 at 11:13 UTC
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Identifing all filenames over 250 letters long
by merlyn (Sage) on Jul 30, 2009 at 15:53 UTC
    It's not clear whether you want the "filename" over 250 chars, or the entire "filepath" over 250 chars, so here's both:
    use File::Finder; # in the CPAN my @long_names = File::Finder->eval(sub { length > 250 })->in('/'); my @long_paths = File::Finder->eval(sub { length $File::Find::name > 2 +50})->in('/');

    -- Randal L. Schwartz, Perl hacker

    The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

    A reply falls below the community's threshold of quality. You may see it by logging in.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Identifing all filenames over 250 letters long
by BrowserUk (Patriarch) on Jul 30, 2009 at 15:29 UTC

    Assuming you're on Win* and like simple:

    dir /s /b c:\* | perl -nE"length() > 250 && say"

    Or if you want those with just filenames over 250 chars rather than the whole path:

    dir /s /b c:\* | perl -nE"/\\([^\\]+)$/ and length($1) > 250 && say"

    (Adjust as appropriate for non-5.10 builds.)


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      I of course like this one liner! In this case however, I think just print is better
      dir /s /b c: | perl -nE"length($_) > 10 && print"
      as the dir command already puts in new lines so the extra one by "say" (Note to OP: "say" is the 5.10 feature) could be distracting.

Re: Identifing all filenames over 250 letters long
by Marshall (Canon) on Jul 30, 2009 at 15:20 UTC
    I had some code and I just hacked some changes..this is a prototype of how to use File::Find. The find() function will visit every file below the starting path that you specify and it will call a subroutine for each of those files. Note that a directory is a FILE! This sub cannot return any values except by manipulating something at a higher lexical scope.

    The print_all_files subroutine doesn't do anything if it sees a plain file. If it sees a directory, it opens it and reads the names and does something with them. The code below is a bit strange as there is a provision to keep going if a directory doesn't open. There was a different file handle than STDOUT, but sdtdout will get you started. I didn't know if you meant total file path >250 chars? or actual file name, although I must say that a 250 char actual file name is extreme! You can change the grep below to select what you want. The original print statement was more complex, but you can just leave the map{} in there.

    I probably would do some small things differently now if I was starting from scratch, but it runs and does illustrate the basic idea of how to use find(). Basically run this code, see what it does and then adapt to what you need. I leave the polishing up to you.

    #!/usr/bin/perl -w use strict; use File::Find; my $ROOT_DIR = "C:/"; #something like "$ENV{'HOME'}" on Unix.. find (\&print_all_files, "$ROOT_DIR"); sub print_all_files { return if !-d || !stat; #on some OS'es, this stat is needed my $full_dir_path = $File::Find::name; print STDOUT "$full_dir_path\n","-" x length($full_dir_path),"\n"; if (!opendir (D,$_)) { print STDERR "Could not open directory $full_dir_path\n\n"; die; #or just do a return() to keep going.... } print STDOUT map { sprintf "%s \n",$_; } #grep {length($_)>250} #maybe here? #grep {length("$full_dir_path/$_") >250) maybe? sort grep{ !-d "$full_dir_path/$_"}readdir D; print STDOUT "\n"; close D; }
    Example output:
    C:/91836f37983a8346dad3/i386 ---------------------------- filterpipelineprintproc.dll msxpsdrv.cat msxpsdrv.inf msxpsinc.gpd msxpsinc.ppd mxdwdrv.dll xpssvcs.dll C:/Calendar ----------- calfaq.zip