Author Topic: [Java] Finding a file in a case-insensitive way  (Read 10271 times)

0 Members and 1 Guest are viewing this topic.

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
[Java] Finding a file in a case-insensitive way
« on: January 26, 2006, 08:58:53 pm »
One little problem with Windows is that nothing is case sensitive.  As a result, people don't realize that case matters and it is often a source of screw-up on other operating systems.  So I wrote this code which checks if a file exists; if it doesn't, it gets a directory listing and checks if each file matches in a case-insensitive way.  Not the fastest way to do it, but I can't think of a better way. 

Quote
    /** Attempts to find a file in a case-insensitive way.  I suggest avoiding over-using this.  Saving the 
     * new filename somewhere and re-using it is probably the best way to use this function.
     *
     * @param filename The full path to the file. 
     * @return The best guess at the case-insensitive filename.  It will return the original filename if it
     *  is unable to find anything better.  If there are multiple files with the same name and different
     *  cases, there is no guarentee as to which one is returned.
     */
    private static String findFile(String filename)
    {
       File file = new File(filename);
       File directory;
       String []files;
       
       if(file.exists())
          return filename;

       /* Get the directory listing */
       directory = file.getParentFile();
       files = directory.list();
       for(int i = 0; i < files.length; i++)
       {
          if(files.equalsIgnoreCase(file.getName()))
             return directory.getAbsolutePath() + "/" + files;
       }

       /* If it couldn't find it, just return the name we were given */
       return filename;
    }

And here's an example of using it:
Quote

   public static void main(String []args)
   {
      System.out.println(findFile("/home/iago/.hashes/STAR/STarCrAFT.eXe"));
   }

Which outputs exactly what you'd expect:
Quote
/home/iago/.hashes/STAR/starcraft.exe

Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: [Java] Finding a file in a case-insensitive way
« Reply #1 on: January 26, 2006, 09:13:21 pm »
One little problem with Windows is that nothing is case sensitive.
Uh uh, that's not true.  Case sensitivity is supported by NTFS and as well as the POSIX subsystem of Windows.
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: [Java] Finding a file in a case-insensitive way
« Reply #2 on: January 26, 2006, 09:52:41 pm »
One little problem with Windows is that nothing is case sensitive.
Uh uh, that's not true.  Case sensitivity is supported by NTFS and as well as the POSIX subsystem of Windows.
I've never seen case-sensitive Windows files.  Is there some secret to enabling them? 

I've never officially checked if it was possible, but by default it doesn't happen, so that's the important part. 

The problem is, in bots, some people package the hash files with different cases than what I expect, because on Windows, by default, it doesn't matter. 

Offline rabbit

  • x86
  • Hero Member
  • *****
  • Posts: 8092
  • I speak for the entire clan (except Joe)
    • View Profile
Re: [Java] Finding a file in a case-insensitive way
« Reply #3 on: January 26, 2006, 10:57:40 pm »
Supported != Matterful

It's true that NTFS supports case-sensitivity, but Windows overlooks that fact because it realizes that 97% of it's users would be too stupid to realize that somefile isn't the same as SOMEFILE, and start deleting "duplicates".

Offline deadly7

  • 42
  • Moderator
  • Hero Member
  • *****
  • Posts: 6496
    • View Profile
Re: [Java] Finding a file in a case-insensitive way
« Reply #4 on: January 27, 2006, 07:06:12 pm »
Yeah, pretty much what R.a.B.B.i.T said.
Also, that's not *exactly* true, I've had the command prompt window bitch at me for not having "Documents and Settings" capitalized like that, for some reason.
[17:42:21.609] <Ergot> Kutsuju you're girlfrieds pussy must be a 403 error for you
 [17:42:25.585] <Ergot> FORBIDDEN

on IRC playing T&T++
<iago> He is unarmed
<Hitmen> he has no arms?!

on AIM with a drunk mythix:
(00:50:05) Mythix: Deadly
(00:50:11) Mythix: I'm going to fuck that red dot out of your head.
(00:50:15) Mythix: with my nine

Offline Sidoh

  • Moderator
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [Java] Finding a file in a case-insensitive way
« Reply #5 on: January 27, 2006, 09:26:45 pm »
Yeah, pretty much what R.a.B.B.i.T said.
Also, that's not *exactly* true, I've had the command prompt window bitch at me for not having "Documents and Settings" capitalized like that, for some reason.

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\>cd docuMENTS and SeTTinGS

C:\Documents and Settings>cd ../DOCUMENTS AND SETTINGS

C:\Documents and Settings>


I suspect you were using a command that had more than one parameter, like copy:

C:\>echo "" > "Documents and Settings\test.txt"

C:\>dir Documents
 Volume in drive C has no label.
 Volume Serial Number is D424-0774

 Directory of C:\

01/27/2006  07:31 PM                28 Documents
               1 File(s)             28 bytes
               0 Dir(s)  12,835,221,504 bytes free
C:\>echo "" > "Documents and Settings\test.txt"

C:\>dir Documents and Settings\test.txt
The system cannot find the file specified.

C:\>dir "Documents and Settings\test.txt"
 Volume in drive C has no label.
 Volume Serial Number is D424-0774

 Directory of C:\Documents and Settings

01/27/2006  07:32 PM                 5 test.txt
               1 File(s)              5 bytes
               0 Dir(s)  12,835,221,504 bytes free



As you can see, unless you enclose a directory that contains spaces in quotes, it is truncated after the first space.  I highly suspect that's the reason you were having issues with 'casing.'

By default, NTFS is totally non-case sensitive.  It was weird seeing files with the same file and different casing when I first set up samba on my server.

Offline deadly7

  • 42
  • Moderator
  • Hero Member
  • *****
  • Posts: 6496
    • View Profile
Re: [Java] Finding a file in a case-insensitive way
« Reply #6 on: January 28, 2006, 10:47:32 am »
That was probably it, Sidoh.. Command prompt blows anyways. :\
[17:42:21.609] <Ergot> Kutsuju you're girlfrieds pussy must be a 403 error for you
 [17:42:25.585] <Ergot> FORBIDDEN

on IRC playing T&T++
<iago> He is unarmed
<Hitmen> he has no arms?!

on AIM with a drunk mythix:
(00:50:05) Mythix: Deadly
(00:50:11) Mythix: I'm going to fuck that red dot out of your head.
(00:50:15) Mythix: with my nine