CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
case-insensative filenames

 



parham_m_s
journeyman

Mar 16, 2001, 8:38 PM

Post #1 of 3 (402 views)
case-insensative filenames Can't Post

i open 3 files on a unix machine (ABC.txt, abc.txt, AbC.txt) and for some reason, they all open different files, not the same one.
Is there a way to make all 3 files be the same? I thought about lowercasing abc to a variable and then opening it, but does anyone have another suggestion?



Jasmine
Administrator / Moderator

Mar 17, 2001, 1:50 AM

Post #2 of 3 (398 views)
Re: case-insensative filenames [In reply to] Can't Post

That's because *nix is case sensitive, so ABC.txt is not the same file as aBc.txt. The easiest way I can think of to perform a case insensitive search of existing files of varying case is like this:


Code
my $searchfor = qr/abc/i; # change abc to whatever 

my @files = grep {-T} glob '/home/yourdomain/data/dirtosearch/*';

for (@files){
print "$_\n" if /$searchfor/;
}

In a nutshell, the above is grabbing the entire contents of the directory you're searching and tossing it in @files then printing the filename only if it matches the search pattern regardless of case.

Assuming you have a match, you can do whatever you need to with the file -- open it, write to it, delete it. You have the full path to the file and the filename as it appears on the server, regardless of the case of the filename.

I'm sure there's a better (and shorter) way to do this, but this is the best I can do at this wee hour Smile

Hope this helps!



parham_m_s
journeyman

Mar 31, 2001, 10:09 AM

Post #3 of 3 (389 views)
Re: case-insensative filenames [In reply to] Can't Post

in an effort to opening files on unix systems with filenames that are case-sensative, i started doing this:

Code
$filename = lc($form); 

open (DATA, "$filename.dat");
flock (DATA, 2);
@contents = <DATA>;
flock (DATA, 8);
close (DATA);

after i altered about 26 files, i thought about it for a second.. and well, can i do this?

Code
open (DATA, lc("$form.dat"));  
flock (DATA, 2);
@contents = <DATA>;
flock (DATA, 8);
close (DATA);


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives