tags:

views:

425

answers:

5

I have to make a unix compatible windows delphi routine that confirms if a file name exists in filesystem exactly in same CaSe as wanted, e.g. "John.txt" is there, not "john.txt".

If I check "FileExists('john.txt')" its always true for John.txt and JOHN.TXT due windows .

How can I create "FileExistsCaseSensitive(myfile)" function to confirm a file is really what its supposed to be.

DELPHI Sysutils.FileExists uses the following function to see if file is there, how to change it to double check file name is on file system is lowercase and exists:

function FileAge(const FileName: string): Integer;
var
  Handle: THandle;
  FindData: TWin32FindData;
  LocalFileTime: TFileTime;
begin
  Handle := FindFirstFile(PChar(FileName), FindData);
  if Handle <> INVALID_HANDLE_VALUE then
  begin
    Windows.FindClose(Handle);
    if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then
    begin
      FileTimeToLocalFileTime(FindData.ftLastWriteTime, LocalFileTime);
      if FileTimeToDosDateTime(LocalFileTime, LongRec(Result).Hi,
        LongRec(Result).Lo) then Exit;
    end;
  end;
  Result := -1;
end;
+1  A: 

I dealt with this issue a while back, and even if I'm sure that there are neater solutions out there, I just ended up doing an extra check to see if the given filename was equal to the name of the found file, using the case sensitive string comparer...

Banang
any sample code?
Tom
Not in delphi, I'm afraid, but in java it looked something like: if (file.getName().Equals(fileName)) doStuff();
Banang
+1  A: 

I ran into a similar problem using Java. Ultimately I ended up pulling up a list of the directory's contents (which loaded the correct case of filenames for each file) and then doing string compare on the filenames of each of the files.

It's an ugly hack, but it worked.

Edit: I tried doing what Banang describes but in Java at least, if you open up file "a.txt" you'r program will stubbornly report it as "a.txt" even if the underlying file system names it "A.txt".

Kris
Looks like we're riding the same ship, Kris... :)
Banang
+7  A: 
function FileExistsEx(const FileName: string): Integer;
var
  Handle: THandle;
  FindData: TWin32FindData;
  LocalFileTime: TFileTime;
begin
  Handle := FindFirstFile(PChar(FileName), FindData);
  if Handle <> INVALID_HANDLE_VALUE then
  begin
    Windows.FindClose(Handle);
    if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then
    begin
      FileTimeToLocalFileTime(FindData.ftLastWriteTime, LocalFileTime);
      if FileTimeToDosDateTime(LocalFileTime, LongRec(Result).Hi, LongRec(Result).Lo) then
        if AnsiSameStr(FindData.cFileName, ExtractFileName(FileName)) then Exit;
    end;
  end;
  Result := -1;
end;


Tom, I'm also intrigued by your use case. I tend to agree with Motti that it would be counter intuitive and might strike your users as odd.

Lieven
FileExistsEx returns an integer? Hm the example in the question used FileAge.
Gamecat
@Gamecat - I know. I used and changed the FileAge implementation and tried refactoring it to returning a Boolean. It soon got messy. The FileTimeToDosDateTime uses the Result and expects it to be an integer. I know, declare a local variable to pass etc... Left that as an excercise for the reader.
Lieven
Just a small point, butg iven a file "C:\Folder\File.txt"; FileExistsEx("c:\fOlDeR\File.txt") will return true, which may not be what you wanted...
Roddy
A: 

You can implement the approach mention by Kris using Delphi's FindFirst and FindNext routines.

See this article

dommer
+4  A: 

On windows file names are not case sensitive so I don't see what you can gain from treating file names as if they were case sensitive.

In any case you can't have two files named "John.txt" and "john.txt" and failing to find "John.txt" when "john.txt" exists will probably result in very puzzled users.

Trying to enforce case sensitivity in this context is un-intuitive and I can't see a viable use-case for it (if you have one I'll be happy to hear what it is).

Motti