Friends,
I am trying to write a script that will take a directory as an argument and look to files that have unwanted characters in their names. For example the scrirpt would work like this ...
bash-2.03$ ls -l DIR/
total 4
-rw------- 1 pk ton 180 Dec 1 12:45 junk
-rw------- 1 pk ton 20 Dec 1 12:45 rm -f .
bash-2.03$
bash-2.03$ ./testFNI.pl DIR/
./testFNI.pl : ILLEGAL FILE NAME in DIR [rm -f .]
Here is what I have tried so far ...
#!/usr/local/bin/perl -w
use strict;
use File::Copy;
use File::Basename;
sub illegalFile {
my $dir=shift;
#look for dangerous file name
#for my $FN ( glob( '$dir/*"[ |\~|\?|\<|\>|\,|\`|\!|\@|\#|\%|\^|\&|\*
+|\||\(|\)|\;|\+|\=|\{|\}|\\|\[|\]|\-]"*' )) {
#for my $FN ( glob( "$dir/*[ \~\?\<\>\,\`\!\@\#\%\^\&\*\|\(\)\;\+\=\{
+\}\\\[\]\-]*" )) {
my $pat = $dir . "/" . quotemeta ("*[ ~?<>,`!@#%^&*|();+={}\[]-");
for my $FN ( glob $pat ) {
print "$0 : ILLEGAL FILE NAME in $dir [$FN]\n";
my $PATH_HAVING_ILLEGAL_FILE=dirname $dir;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isds
+t) = localtime(time);
my $DIR_HAVING_ILLEGAL_FILE="$PATH_HAVING_ILLEGAL_FILE
+/ILLEGAL_FILE_NAME_" . $year
. "_" . $mon
. "_" . $mday
. "_" . $hour
. "_" . $min
. "_" . $sec;
mkdir $DIR_HAVING_ILLEGAL_FILE;
move( $dir, $DIR_HAVING_ILLEGAL_FILE );
mkdir $dir;
mailError ("illegal file name detected!");
exit 1;
}
### END look for dangerous file name
}
my $dir = shift;
if ( !$dir ) { die "usage : $0 <dir>\n"; }
illegalFile ($dir);
... this code doesn't find the bad file name. Other things I have tried think every file is bad!
| Plankton: 1% Evil, 99% Hot Gas. |