Re: About the \r bug in Ascii Pfiles

From: Patrick Dughi (dughi@imaxx.net)
Date: 08/18/01


> I'm wondering if there's an easy (ie. non-manual) way of removing files
> containing special chars. I've had the bug with the DOS pfiles, causing
> a lot of files to be created with special chars in the filename:
>
ugly way, but I can't think of anything else right off hand.  Code to
follow... anyone feel free to one-up me with something simple.

keep in mind this is mailer code, but even without knowledge of perl, i
think you could fix any errors.. just make sure to set your bad_directory
variable.

                                                PjD



#!/usr/bin/perl
if(@ARGV != 1 || !(-d $ARGV[0])) {
  print "Need a directory argument chum.\n";
  exit;
}

&fix_dir($ARGV[0]);

sub fix_dir {
  my $curdir = shift;


  my $bad_directory = "ASSIGN A BAD DIRECTORY HERE!!!!";

  if($bad_directory eq "ASSIGN A BAD DIRECTORY HERE!!!!") {
    print "Yo! Edit this script, search for 'bad_directory' and set\n";
    print "  the dir you want your bad files to go to!\n";
    exit;
  }
  if(-e $bad_directory && !(-d $bad_directory)) {
    print "'$bad_directory' is not a directory!\n";
    exit;
  }
  if(!(-e $bad_directory)) {
    if(!mkdir($bad_directory)) {
      print "Failed to create dir '$bad_directory', exiting.\n";
      exit;
    }
    print "Created dir '$bad_directory'\n";
  }

  opendir(DIR,"$curdir");

  # works to get rid of the . and .. dirs, regardless of the dir you're
  # in.. works in windows too. sorry if it looks confusing but this IS
  # the easiest way I could think of.

  my @list = grep !/[\\\/]?\.\.?$/, map "$curdir/$_", readdir DIR;

  foreach $file (@list) {
    if(-d $file) {

      #&fix_dir($curdir);
    } elsif (-f $file) {
      my $fn = $file;

        # put in the chars you want.. it removes them..
        # therefore, if you don't want things like spaces, etc..
        # you can replace this with something like..
        # $fn =~ s/[A-Za-z0-9\.\\\/_-]*//g; and get most 'legal'
        # filenames. remember, you're geting a pseudo absolute filename
        # relative to the argument you provided ($ARGV) so, you may
        # have to include \'s or /'s (like above example) or even :'s
        # the trick is to remove all chars you like. if the string is
        # zero lenght after that, you're set, else it's bad.

        # this one looks for all printable characters

      $fn =~ s/[\x20-\x7E]*//g;
      if(length($fn) != 0) {
        printf("'$file' has bad chars\n);
        `move $file $bad_directory`;
      }
    }
  }
}

--
   +---------------------------------------------------------------+
   | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
   | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
   +---------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/06/01 PST