?
Solved

full path for file

Posted on 2004-11-04
14
Medium Priority
?
299 Views
Last Modified: 2010-04-05
Problem:

I want to be able to create a file but allow the user to choose where to have it created.  I have the routine made that allows them to select the directory but they can also enter it manually by typing it into an edit box.

What i want to be able to do is make sure that if the user enters something like foldername\subfolder that i can change it to include a drive name so that it becomes c:\foldername\subfolder

any ideas?
0
Comment
Question by:foxjax
  • 4
  • 4
  • 3
  • +3
14 Comments
 
LVL 8

Expert Comment

by:BdLm
ID: 12492340
do you know the SaveFile - Dialog from the compent palette?
May be this will solve for request with a line of code
B.
0
 
LVL 26

Expert Comment

by:Tomas Helgi Johannsson
ID: 12492362
   Hi

Try this

...
if ExtractFileDrive(pathfilestr) = '' then
   pathfilestr := strDrive + pathfilestr;
...

Regards,
  Tomas Helgi
0
 

Author Comment

by:foxjax
ID: 12492364
i use that already but since i also allow the user to type in a path in an edit box i need to be able to ensure it will work
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.

 
LVL 8

Expert Comment

by:BdLm
ID: 12492388
Example fpr the dialog:


procedure TForm1.btnSaveClick(Sender: TObject);
begin
 

 if SaveDialog1.Execute then begin
   
   //  Memo  save to file ....
     Memo1.Lines.SaveToFile  (SaveDialog1.FileName + '.txt');
   end;


end;

0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 12492396
as bdlm said, use the TSaveFileDialog-component
0
 
LVL 17

Accepted Solution

by:
Wim ten Brink earned 500 total points
ID: 12492402
Use ExpandFileName or ExpandUNCFileName to just expand whatever the user enters to a full path. ExpandUNCFileName is a bit more useful since it will expand to the format '\\<servername>\<sharename>' if a network file is entered. Both functions will use the current folder as base folder to set up the path. (Which you can change by using CHDir)
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12492431
procedure TForm1.Button1Click(Sender: TObject);
var
  B:      Boolean;
  P:      Integer;
  S:      string;
begin
  B := True;
  S := EditPath.Text;
  P := Pos('\', S);
  case P of
    0: S := 'C:\' + S;
    1: S := 'C:' + S;
    2: S := 'C' + S;
    else B := False;
  end;
  if B then
    EditPath.Text := S;
end;
0
 

Author Comment

by:foxjax
ID: 12492543
>Use ExpandFileName or ExpandUNCFileName ...

Thanks, this looks as it might be what i am after.

> as bdlm said, use the TSaveFileDialog-component

I already use that to allow the user to select the folder BUT i also allow the user to type in the path they want into an edit box - it is when the user types a path into the edit box that i loose control over what they do so i wanted a method to do some of the work without having to bother the user with error messages such as "path does not exist" and the like.

> esoftbg

Thanks, but that idea only covers the C drive - users may have more than just a C drive and expanding the code you gave would prove a pain - ie: i have drives C thru M on my system (300Gb worth of dives plus CDROM's, etc)
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12492626
> ie: i have drives C thru M on my system (300Gb worth of dives plus CDROM's, etc)

I have drives from A: to Z: and sometimes there are gaps between them. But since I have several USB disk devices, it is well possible for me to end up with 26 different drives on my system! (Although I never got higher than 24...) Even funnier, I have a special memory card reader (for memory sticks and other flashcards) and when I connect it, it will add 4 drives to my system! One drive for every flashcard type. Plus three harddisks, one CD-rewriter and a DVD-rewriter, a ZIP drive and still an old-fashioned floppy drive...

Just keep in mind that Expand(UNC)FileName will expand filenames relative from the current folder. But it will also expand things like 'C:File.txt' to the current folder for the C: drive.
0
 
LVL 8

Expert Comment

by:BdLm
ID: 12492846
if you need the list of valids drive :


function GetDriveList : TStringList;
var  DriveList  :  TStringList;
     I,Typ      :  Integer;
     s          :  String;
begin

  try
  DriveList:= TStringList.Create;
  DriveList.Clear;

  For i := 0 To 25 do
    begin
      s := Chr(i + 65) + ':\';
      typ := GetDriveType(PChar(s));
      If Typ <> 0 Then
        Case Typ of
          DRIVE_REMOVABLE :
            DriveList.Add(Chr(i + 65) + ': Diskette');

          DRIVE_FIXED :
            DriveList.Add(Chr(i + 65) + ': Festplatte');

          DRIVE_CDROM :
            DriveList.Add(Chr(i + 65) + ': CDROM');

          DRIVE_RAMDISK :
            DriveList.Add(Chr(i + 65) + ': RAMDisk');

          DRIVE_REMOTE :
            DriveList.Add(Chr(i + 65) + ': Netzlaufwerk');
        end;
    end;
    finally   { Listenobjekt freigeben }
    Result :=  DriveList;

    end;

end;
0
 
LVL 8

Expert Comment

by:BdLm
ID: 12492858
of course remo ve the unnneeded comments ...
and skip the case command:

               DriveList.Add(Chr(i + 65));
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12493608
BlDm, that won't support any network drives with a format of \\Server\Share\Filder\File.txt while ExpandUNCFileName is well aware of network folders. Besides, when a user enters a relative path, the user will expect this path to be relative from the working folder. Besides, why make it that complicated when it could be done quite easily?
0
 

Author Comment

by:foxjax
ID: 12494978
ExpandFileName did the trick - i went with ExpandFileName since i do not allow saving to a network drive....at the moment, so ExpandUNCFileName will come in handy later perhaps.

Thanks
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12495038
You're welcome.
And well, ExpandUNCFileName is just useful in the future indeed. It's just something to remember.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Screencast - Getting to Know the Pipeline
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month15 days, 13 hours left to enroll

850 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