• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

Find the real (case-sensitive) name of a Windows directory

Windows is not case-sensitive for file and directory names but it preserves the case.
I need in a Delphi program to be able to verify the existence of a directory but also the fact that the name I check respects the case of the existing directory.
My code will looks like :

var
 Dir:string;
 .../...
begin
 .../...
 if DirectoryExists(Dir)
   then if TheNameIsTheSame(Dir) // hypothetical function ....

The question is that I don't know how to check that Dir respects the existing directory case
Better, I want to get from Windows the exact name of the directory
0
LeTay
Asked:
LeTay
1 Solution
 
ziolkoCommented:
GetWindowsDirectory()

ziolko.
0
 
ziolkoCommented:
procedure TForm1.Button2Click(Sender: TObject);
var pth: string;
begin
  SetLength(pth, MAX_PATH);
  GetWindowsDirectory(PChar(pth), MAX_PATH);
  ShowMessage(pth);
end;

ziolko.
0
 
ziolkoCommented:
..or even better:
procedure TForm1.Button2Click(Sender: TObject);
var pth: array[0..MAX_PATH] of Char;
begin
  FillChar(pth, SizeOf(pth), 0);
  GetWindowsDirectory(@pth[0], MAX_PATH);
  ShowMessage(pth);
end;

ziolko.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LeTayAuthor Commented:
Sorry, I don't need the name of "the" Windows directory
I need, having in a string variable the name of an existing directory, to find the real case-sensitive name of this directory
For example, if on my C: drive there is a directory named C:\BlaBla
and if in a variable I have 'C:\blabla',
I need to be able to "convert" the variable content to 'C:\BlaBla'
0
 
diniludCommented:
i didn't get you
0
 
MerijnBSr. Software EngineerCommented:
var MatchFound: TFilenameCaseMatch;
begin
 Caption := ExpandFileNameCase('c:\PrOgRaM fIlEs', MatchFound);
end;
0
 
ziolkoCommented:
ahhh sorry for misunderstanding.

you may try FindFirst() FindNext() with faDirectory

ziolko.
0
 
diniludCommented:
function GetParentDir(dir: String): String;
begin
   try
     if dir[length(dir)]='\' then Delete(dir,length(dir),1);
     Result := ExcludeTrailingBackSlash (ExtractFileDir (dir));
   except
     Result := #0;
   end;
end;

function TheNameIsTheSame(Dir:String):Boolean;
var CurrentDirName,CurrentDirNameW:string;
    Flag: TFilenameCaseMatch;
begin
    Result:=False;
    if dir[length(dir)]='\' then Delete(dir,length(dir),1);
    CurrentDirName:=copy(dir,length(GetParentDir(dir))+2,length(dir));
    CurrentDirNameW:= ExpandFileNameCase(dir, Flag);
    CurrentDirNameW:=copy(CurrentDirNameW,
         length(GetParentDir(CurrentDirNameW))+2,length(CurrentDirNameW));
    Result:=CurrentDirNameW=CurrentDirName;
end;
0
 
diniludCommented:
What about

"C:\program files\borland" or "C:\Progra~1\Borland"
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now