How to quickly verify that a case-sensitive file name does exist

I need to make a unix-compatible windows delphi procedure, which confirms whether the file name on the file system exists in exactly the same CaSe as required, for example. "John.txt" is, not "john.txt".

If I check "FileExists (" john.txt "), it is always true for the John.txt and JOHN.TXT windows.

How can I create a FileExistsCaseSensitive (myfile) function to confirm that the file is really what it should be.

DELPHI Sysutils.FileExists uses the following function to see if there is a file, how to change it, to double-check the file name in the file system, has lowercase letters 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;
+3
5
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;

, . Motti, .
+7

Windows , , , .

"John.txt" "John.txt" "John.txt", "John.txt" , , .

, ( , , ).

+4

, , , , , , , ...

+1

Java. ( ), .

, .

Edit: I tried to do what Banang describes, but at least in Java, if you open the β€œa.txt” file, your program will stubbornly report it as β€œa.txt”, even if the underlying file system calls his "a.txt".

+1
source

You can implement the mention of the approach by Chris using the Delphi FindFirst and FindNext procedures.

See this article

0
source

Source: https://habr.com/ru/post/1705563/


All Articles