SPUG: Checking for ASCII files
Ben Burnett
benwa at ocentrix.net
Tue May 15 15:05:45 CDT 2001
Lorraine,
I think you can user Perl's file test operators to do this.
"-T"
will tell you if a file is plain ASCII text.
for example:
---(isascii.pl)---
#!/usr/bin/perl
use strict;
my $file = shift;
if ( -T $file )
{
print "$file is an ascii file.\n";
}
else
{
print "$file is NOT an ascii file.\n";
}
---(/isascii.pl)---
This little script takes one filename as an argument and
tells you if it is an ascii file.
running it yeilds:
[ben at va01 ben]$ perl isascii.pl myfile.txt
myfile.txt is an ascii file.
[ben at va01 ben]$ perl isascii.pl medup.jpg
medup.jpg is NOT an ascii file.
[ben at va01 ben]$
"perldoc -f -T" at the command line should tell you all
about the file test operators.
-Ben
------- Original Copy -------
>Subject: RE: SPUG: Checking for ASCII files
>Date: 05/15/2001 12:16 PM
>From: Lorraine Johnson <lorraine.johnson at elektrobit.com>
>To: "'James Moore'" <james at banshee.com>,Lorraine
Johnson<lorraine.johnson at elektrobit.com>
>Cc: "'spug-list at pm.org'" <spug-list at pm.org>
>Thanks James, we'll give that a try.
>
>Our projects have a quite a few files. On my machine, Perl
lists out at
>2198 files and 296 folders. We tend to be about that size.
>
>Lorraine
>
>-----Original Message-----
>From: James Moore [mailto:james at banshee.com]
>Sent: Tuesday, May 15, 2001 12:07 PM
>To: Lorraine Johnson
>Cc: 'spug-list at pm.org'
>Subject: Re: SPUG: Checking for ASCII files
>
>
>Consider calling the UNIX "file" utility (usually found in
>/usr/bin/file) from your script. Not a great idea if
you've got huge
>numbers of files (since you're going to be forking and
exec'ing too
>often), but you may find that some of the other facilities
offered by
>file would be useful.
>
>James Moore
>james at banshee.com
>
>Lorraine Johnson wrote:
>
>> What is the best way to determine if a file
>> is ASCII or non-ASCII?
>>
>
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - -
> POST TO: spug-list at pm.org PROBLEMS:
owner-spug-list at pm.org
> Subscriptions; Email to majordomo at pm.org: ACTION
LIST EMAIL
> Replace ACTION by subscribe or unsubscribe, EMAIL by your
Email-address
> For daily traffic, use spug-list for LIST ; for weekly,
spug-list-digest
> Seattle Perl Users Group (SPUG) Home Page:
http://www.halcyon.com/spug/
>
>
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
POST TO: spug-list at pm.org PROBLEMS: owner-spug-list at pm.org
Subscriptions; Email to majordomo at pm.org: ACTION LIST EMAIL
Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
For daily traffic, use spug-list for LIST ; for weekly, spug-list-digest
Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
More information about the spug-list
mailing list