full path for file

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?
foxjaxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BdLmCommented:
do you know the SaveFile - Dialog from the compent palette?
May be this will solve for request with a line of code
B.
0
Tomas Helgi JohannssonCommented:
   Hi

Try this

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

Regards,
  Tomas Helgi
0
foxjaxAuthor Commented:
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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

BdLmCommented:
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
kretzschmarCommented:
as bdlm said, use the TSaveFileDialog-component
0
Wim ten BrinkSelf-employed developerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
esoftbgCommented:
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
foxjaxAuthor Commented:
>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
Wim ten BrinkSelf-employed developerCommented:
> 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
BdLmCommented:
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
BdLmCommented:
of course remo ve the unnneeded comments ...
and skip the case command:

               DriveList.Add(Chr(i + 65));
0
Wim ten BrinkSelf-employed developerCommented:
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
foxjaxAuthor Commented:
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
Wim ten BrinkSelf-employed developerCommented:
You're welcome.
And well, ExpandUNCFileName is just useful in the future indeed. It's just something to remember.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.