Solved

Short file names for DOS based utilities

Posted on 1998-10-22
14
140 Views
Last Modified: 2010-04-06
When I retrieve a long file name, how can I retrieve it so that DOS based utilities (third party, retrieved by an achaeologist, but ideal in this instance) can use the file name?

For example MyFileAbcdefPlusMoreStuff has to be passed as MyFile~1 (or whatever is appropriate).
0
Comment
Question by:dejohn99
  • 6
  • 6
  • 2
14 Comments
 
LVL 10

Expert Comment

by:viktornet
ID: 1344046
You can do just like this... If Length(FileName) > 5 then FileName := Copy(FileName, 1, 5) + '~1';

Or here is an original solution to this one....Haven't tested it but it should work.....

function ShortPath( Path : string) : string;
var
  Len : Integer;
  Short : String;
  pShort : PChar;
begin
  Result := '';
  Len := Length(Path);
  pShort := StrAlloc(Len);
  GetShortPathName(PChar(Path), pShort, Len);
  Short := pShort;
  StrDispose(pShort);
  Result := Short;
end;

Cheers,
Viktor
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1344047
Hi dejohn99,

and viktor was first... jsut when I was making a demo...
well, here you got a slight other approach.

procedure TForm1.Button1Click(Sender: TObject);

Function GetShortFileName(Const FileName : String) : String;
var
  aTmp: array[0..255] of char;
begin
  if GetShortPathName(PChar(FileName),aTmp,Sizeof(aTmp)-1)=0 then
     Result:= FileName
  else
     Result:=StrPas(aTmp);
end;

begin
 if OpenDialog1.Execute then begin
  showmessage(getshortfilename(opendialog1.Filename));
 end;
end;

Regards, Zif.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1344048
this one was tested and even cuts the too long paths.
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1344049
This does the same thing....

function ShortPath( Path : string) : string;
var
  pPath : array[0..MAX_PATH] of char;
begin
  GetShortPathName(PChar(Path), pPath, MAX_PATH - 1);
  Result := StrPas(pPath);
end;

Cheers,
Viktor Ivanov
0
 

Author Comment

by:dejohn99
ID: 1344050
Guess what guys, I have tried your codes BUT ...

The path returned by the function is always empty!  Am I missing something???

0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1344051
Have you just pasted my code into a form-unit? And tried it out? It works perfectly here.... What version of Delphi are you using?
Zif.
0
 

Author Comment

by:dejohn99
ID: 1344052
Sorry vik - Zifnab's code helped me more.  Thanks for the assists though.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:dejohn99
ID: 1344053
Zifnab - You truly are a wizard greater than Gandalf - even if he did have a Balrog hanging from his skivvies.  You can even do Dorothy today if you want!

Please resubmit as an answer for your points.
0
 
LVL 8

Accepted Solution

by:
ZifNab earned 50 total points
ID: 1344054
Dejohn99,

I know, I know I'm a greater wizard then Gandalf, it's because I'm Gandalf myself! Oh, nope I'm not, I even don't know a Gandalf... I'm 007... yep that's me... (sorry, couldn't resist... because you refering to Gandalf.... great also a AD&D player, or something else?)

Just wizarding :

Comment
       From: ZifNab
                                                                                   Date: Thursday, October 22 1998 - 03:57PM PDT

       Hi dejohn99,

       and viktor was first... jsut when I was making a demo...
       well, here you got a slight other approach.

       procedure TForm1.Button1Click(Sender: TObject);

       Function GetShortFileName(Const FileName : String) : String;
       var
         aTmp: array[0..255] of char;
       begin
         if GetShortPathName(PChar(FileName),aTmp,Sizeof(aTmp)-1)=0 then
            Result:= FileName
         else
            Result:=StrPas(aTmp);
       end;

       begin
        if OpenDialog1.Execute then begin
         showmessage(getshortfilename(opendialog1.Filename));
        end;
       end;

Regards, Zif.
0
 

Author Comment

by:dejohn99
ID: 1344055
Well if you're 007 today, then watch out for Moneypenney, or was that Q ... hmmm

I'm not a D&D player - I prefer GURPS.

I think we must read some of the same books for fun though.

TTYL   <grin>
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1344056
John, what is GURPS?
0
 

Author Comment

by:dejohn99
ID: 1344057
Generic
Universal
Role
Playing
System

It's a lot simpler, better organized and flexible than D&D - more of a simulations rule set.  Any scenario from prehistoric through science fiction can be played, including sometimes bizzarre combinations like magic versus high-tech versus psionics.  You could conceivably drop a D&D Magic User into a star trek episode, or Captain Kirk into Brahm Stoker's Dracula.  (What would Dr. McCoy make of Vlad Dracula?  "He's undead Jim" ??)

But the purpose of any game is pure fun, so go for it!!!   :-)
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1344058
Hi John, somewhere an url about GURPS?
0
 

Author Comment

by:dejohn99
ID: 1344059
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now