[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do u convert a path to a windows path?

Posted on 2011-04-27
21
Medium Priority
?
543 Views
Last Modified: 2012-06-27
Hi, how do you convert a path to a windows path?

like

C:\program files\

to

C:\program~\

etc
0
Comment
Question by:eNarc
  • 6
  • 5
  • 4
  • +2
18 Comments
 
LVL 12

Accepted Solution

by:
Hypo earned 501 total points
ID: 35476314
Use the GetShortPathName function in the Windows API...


Note that not all folders may have a short path... There is a setting in the OS which controls if short paths should be generated for files and folders... also, files and folders that don't exist can not be converted in this way... so you can not get the short path for a file that you intend to create, before you have actually created it...

/Hypo
function PathToShortPath(APath : string) : string;
begin
  SetLength(Result, GetShortPathName(PChar(APath), nil, 0));
  if Length(Result) <> 0
    then GetShortPathName(PChar(APath), PChar(Result), Length(Result))
    else raiseLastOSError; // This can be replaced with Result := APath, if you don't want it to raise an exception when it can't find a short path...
end;

Open in new window

0
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 35476885
hypo is right, btw I use this function to get a right result anyway
function ShortFileName(FileName: String): String;
var
  aTmp: Array [0..255] Of WideChar;
begin
  If not FileExists(FileName) then
    Result := ''
  else
  begin
    If GetShortPathName(PwideChar(FileName), aTmp, Sizeof(aTmp) - 1) = 0 then
      Result := FileName
    else
      Result := StrPas(aTmp);
  end;
end;

Open in new window

0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35477924

@Ferruccio68

StrPas takes in PAnsiChar as the parameter type
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35477932

Forget it, my bad
0
 
LVL 5

Author Comment

by:eNarc
ID: 35481532
I couldn't get either functions to work.

though this one I found works.

function GetShortFilename(const FileName: TFileName): TFileName;
var
  buffer: array[0..MAX_PATH-1] of char;
begin
  SetString(Result, buffer, GetShortPathName(pchar(FileName), buffer, MAX_PATH-1));
end;
0
 
LVL 12

Expert Comment

by:Hypo
ID: 35481968
I've used that function many times before, and it's always worked for me...

Can you please explain how you did your test, what results you got and what results you expected?

/Hypo
0
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 35482073
Really? This code is working fine for me (Delphi 2010 Pro on Vista Bsns SP2) and return this path

C:\Users\FERRUC~1\DOCUME~1\RADSTU~1\Projects\Project1.exe
procedure TForm1.Button1Click(Sender: TObject);
  function ShortFileName(FileName: String): String;
  var
    aTmp: Array [0..255] Of WideChar;
  begin
    If not FileExists(FileName) then
      Result := ''
    else
    begin
      If GetShortPathName(PwideChar(FileName), aTmp, Sizeof(aTmp) - 1) = 0 then
        Result := FileName
      else
        Result := StrPas(aTmp);
    end;
  end;

begin
  Form1.Caption := ShortFileName(Application.exename);
end;

Open in new window

0
 
LVL 23

Assisted Solution

by:Ferruccio Accalai
Ferruccio Accalai earned 501 total points
ID: 35482083
Ok, there's a test that checks if the file already exists.

To get the Short Path from e directory just remove that check

procedure TForm1.Button1Click(Sender: TObject);
  function ShortFileName(FileName: String): String;
  var
    aTmp: Array [0..255] Of WideChar;
  begin
    If GetShortPathName(PwideChar(FileName), aTmp, Sizeof(aTmp) - 1) = 0 then
      Result := FileName
    else
      Result := StrPas(aTmp);
  end;

begin
  Edit1.text := ShortFileName('C:\program files\'); //C:\PROGRA~1\
end;
0
 
LVL 5

Author Comment

by:eNarc
ID: 35483807
Hypo: when I used it (35476314), it would raise an exception error.


Ferruccio68: when I used it (35476885), it would returned blank.
0
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 35483863
Yes, but (35482083) explains why :)
0
 
LVL 32

Assisted Solution

by:Ephraim Wangoya
Ephraim Wangoya earned 498 total points
ID: 35483991

Let me join in
function ShortPath(const AFileName: WideString): string; overload;
var
  Buffer: array[0..MAX_PATH-1] of WideChar;
begin
  FillChar(Buffer, SizeOf(Buffer), #0);
  if GetShortPathNameW(PWideChar(AFileName), @Buffer[0], SizeOf(Buffer)) > 0 then
    Result := WideString(Buffer)
  else
    Result := ''
end;

function ShortPath(const AFileName: AnsiString): string; overload;
var
  Buffer: array[0..MAX_PATH-1] of AnsiChar;
begin
  FillChar(Buffer, SizeOf(Buffer), #0);
  if GetShortPathNameA(PAnsiChar(AFileName), @Buffer[0], SizeOf(Buffer)) > 0 then
    Result := AnsiString(Buffer)
  else
    Result := ''
end;

Open in new window

0
 
LVL 12

Expert Comment

by:Hypo
ID: 35484422
The thing with short paths is that they only apply to existing folders and files... You can not get a short path from Windows to a path if the actual path does not exist in the file system...

I believe the reason for this is that Windows assigns the short path to the folder when it is created... and you can never forsee what the short path for a path would be just by looking at the path itself without analysing all other paths that might also yield the same short path.

Say you have two paths "C:\Program files" and "C:\Program data"... If you try to predict what the short path for these two paths would be, one by one without taking the other into consideration, then both short paths would be "C:\Progra~1"... but Windows can't have two short paths that have the same name, but points to different files or folders.

So when a new folder or file is created, Windows first have to check if the folder needs a short path in order to be 8.3-compatible; and if it does, Windows then needs to find a short path that is not already occupied by another path... so in the case of the two folders I gave as an example above, the folder that is created first would get the short path "C:\Progra~1", and the folder that is created after that would get the short path "C:\Progra~2", and so on...

I bet that's the reason why the system will never give you a short path to a path that does not exist.    

if you tried the function again with the input path "C:\Program files" instead, I'm positive it would give you the correct short path for that folder...

/Hypo
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35484454

Absolutely, the file has to exist otherwise the function will not work
0
 
LVL 12

Expert Comment

by:Hypo
ID: 35814340
I'd say that eNarc has gotten answers to the question, but I'm not sure that she understands the part about files/folders need to exist on the system for the function to actually give you a short path...

Since she tried the input 35482083 (and I'm assuming here she have no such folder on her system), I feel that it should come as a surprise to no one that the function did not return a short path, given that you understand the part about files/folders need to exist in order to get a short path...

/Hypo
0
 
LVL 101

Expert Comment

by:mlmcc
ID: 35868707
I've requested that this question be deleted for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 35868708
There're  3 possible working solutions here.
Closing and deleting means that every asker could get his answer for free.
I'm not sure that this satisfies all experts that play Ex Ex, anyway not me. I'd prefer a Paq and not Refund, even if the accepted answer would not be mine.
0
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 35872786
0
 
LVL 12

Expert Comment

by:Hypo
ID: 35872892
I agree with Ferruccio; the author has been presented with three working answers to the question along with some additional explanations of how short paths work, and why he/she might not got it to work the first time...

For the sake of keeping these solutions searchable as PAQ's, I'd recommend to accept the three posts Ferruccio suggested as answers to the question, and split the points evenly among those.

/Hypo
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Integration Management Part 2
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month18 days, 6 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question