AdamtheKiwi has asked for the wisdom of the Perl Monks concerning the following question:
I'm writing a little script to run through a (large) list of files and test which of the text files contains ^M characters. In doing this, I've used the -T test to narrow the search down. The code (fragment) looks something like this:
foreach my $folder (@folders) { my $folderfullpath = $componentrootpath . "/" . $folder; print "Working with folder " . $folderfullpath . "\n" if ($debug); opendir(my $folderhandle, $folderfullpath); foreach my $element (readdir($folderhandle)) { next if ($element =~ /^\./); my $quotedfullelementpath = "\"" . $folderfullpath . "/" . $elemen +t . "\""; print "\n *** Full element path: " . $quotedfullelementpath . "\n +" if ($debug); next unless (-T $fullelementpath); open (FILE, "<" . $quotedfullelementpath) or die "Can't open file +$quotedfullelementpath:$!\n"; while (<FILE>) { binmode(FILE); my $line1 = unpack("H*", $_); if ($line1 =~ /0d/) { # File is contaminated; print $quotedfullelementpath . " - contaminated\n" if ($debug) +; my ($elementrootname, $elementuuid) = &nameanduuid($fullelemen +tpath); push (@contaminateduuids, $elementuuid) unless ($contaminatedu +uids{$elementuuid}); $contaminateduuids{$elementuuid} = 1; last; } } close (FILE); } }
I introduced the quotes because one of the teams whose code I'm reporting on uses spaces in their filenames. Unfortunately, the quotes seem to nullify the -T test. I tried adding in this piece of debug code:
my $quotedfullelementpath = "\"" . $folderfullpath . "/" . $elemen +t . "\""; my $singlefullelementpath = "'" . $folderfullpath . "/" . $element + . "'"; my $fullelementpath = $folderfullpath . "/" . $element; print "-> " .$quotedfullelementpath . " is "; print "NOT " unless (-T $quotedfullelementpath); print "a text file\n"; print "-> " .$singlefullelementpath . " is "; print "NOT " unless (-T $singlefullelementpath); print "a text file\n"; print "-> " .$fullelementpath . " is "; print "NOT " unless (-T $fullelementpath); print "a text file\n";
and got this output (fragment):
-> "<shortened-path>/ra6535_5.ddl" is NOT a text file -> '<shortened-path>/ra6535_5.ddl' is NOT a text file -> <shortened-path>/ra6535_5.ddl is a text file -> "<shortened-path>/ra6535_6.ddl" is NOT a text file -> '<shortened-path>/ra6535_6.ddl' is NOT a text file -> <shortened-path>/ra6535_6.ddl is a text file -> "<shortened-path>/ra6535_7.ddl" is NOT a text file -> '<shortened-path>/ra6535_7.ddl' is NOT a text file -> <shortened-path>/ra6535_7.ddl is a text file -> "<shortened-path>/ra6536_5.ddl" is NOT a text file -> '<shortened-path>/ra6536_5.ddl' is NOT a text file -> <shortened-path>/ra6536_5.ddl is a text file -> "<shortened-path>/ra6536_6.ddl" is NOT a text file -> '<shortened-path>/ra6536_6.ddl' is NOT a text file -> <shortened-path>/ra6536_6.ddl is a text file
Clearly, these are all text files.
What gives? Is there another test I could use?
Thanks for for your help - Adam...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: File test -T (text) and quoted filenames
by jethro (Monsignor) on Feb 11, 2011 at 12:47 UTC | |
by ww (Archbishop) on Feb 11, 2011 at 13:55 UTC | |
|
Re: File test -T (text) and quoted filenames
by bart (Canon) on Feb 11, 2011 at 13:04 UTC | |
|
Re: File test -T (text) and quoted filenames
by cdarke (Prior) on Feb 11, 2011 at 13:34 UTC | |
|
Re: File test -T (text) and quoted filenames
by Khen1950fx (Canon) on Feb 11, 2011 at 13:42 UTC | |
|
Re: File test -T (text) and quoted filenames
by jwkrahn (Abbot) on Feb 11, 2011 at 18:30 UTC | |
|
Re: File test -T (text) and quoted filenames
by TomDLux (Vicar) on Feb 17, 2011 at 05:02 UTC |