Skip to main content
2 of 3
Better answer.
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

For a Perl solution, you could use the Win32::Filenames module for this.

http://search.cpan.org/~bch/Win32-Filenames-0.01/lib/Win32/Filenames.pm

EDIT: A simple program using this module:

#!/usr/bin/env perl

use strict;
use warnings;

use Win32::Filenames qw(validate sanitize);

while ( my $filename = shift ) {
    printf( "checking filename [%s]\t", $filename );

    if ( validate($filename) ) {
        print("ok.\n");
    }
    else {
        printf( "failed, try this instead: %s\n", sanitize($filename) );
    }
}

You invoke it with one or several file names on the command line:

$ ./check.pl this that the/other
checking filename [this]        ok.
checking filename [that]        ok.
checking filename [the/other]   failed, try this instead: the-other

For a whole file hierarchy:

$ find . -exec basename {} \; | xargs ./check.pl
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k