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. |
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.