3

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;
Tom
  • 6,725
  • 24
  • 95
  • 159

5 Answers5

7
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 Keersmaekers
  • 57,207
  • 13
  • 112
  • 146
  • FileExistsEx returns an integer? Hm the example in the question used FileAge. – Toon Krijthe Mar 31 '09 at 05:47
  • @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 Keersmaekers Mar 31 '09 at 07:00
  • 1
    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 Sep 30 '09 at 19:27
4

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
  • 110,860
  • 49
  • 189
  • 262
  • I have one. I'm using a macro which opens a directory from folder browser dialog. The macro only works when the given path is absolutely equal in cases. However, my machine has a directory "Binary" and my friends machine "binary". For several reasons I can neither change the macro nor the directory. So I need to validate the path if it's case equal before using it ;) http://stackoverflow.com/questions/16183788/case-sensitive-directory-exists-file-exists – theknut May 15 '13 at 12:52
1

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...

Mia Clarke
  • 8,134
  • 3
  • 49
  • 62
  • Not in delphi, I'm afraid, but in java it looked something like: if (file.getName().Equals(fileName)) doStuff(); – Mia Clarke Mar 30 '09 at 13:15
1

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
  • 14,426
  • 7
  • 55
  • 65
0

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

See this article

dommer
  • 19,610
  • 14
  • 75
  • 137