Solved

How to change current dir

Posted on 2001-09-06
8
491 Views
Last Modified: 2010-04-06
After the current directory has been changed by selecting a file using an Opendialog how can I change it back to the dir where the program is.... the dir of application.exename  ?

begin
OpenDialog1.Execute;
MyStream.SaveToFile(OpenDialog1.Filename);
//now how do I set current dir back
//to the programs dir so that the following line
//saves another copy of MyStream as 'test.txt'
//in the same dir that the program is in
MyStream.SaveToFile('test.txt');
end;

0
Comment
Question by:Gwena
  • 3
  • 2
  • 2
  • +1
8 Comments
 

Expert Comment

by:pin_plunder
ID: 6462721
// this saves the file test.txt in the dir where the executable is stored
MyStream.SaveToFile(ExtractFilePath(Application.ExeName) + 'test.txt');
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 6462741
this is how you get the dir of the current executable

ExtractFilePath(Application.ExeName);

e.g.

if OpenDialog1.Execute then
begin
  MyStream.SaveToFile(OpenDialog1.FileName);
  MyStream.SaveToFile(ExtractFilePath(Application.ExeName) + 'Test.txt');
  // or if you want to save it with the same name as in OpenDialog1, but in different path:
  MyStream.SaveToFile(ExtractFilePath(Application.ExeName) + ExtractFileName(OpenDialog1.FileName));
end;


Hope that helps!



DragonSlayer.
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 6462748
oops pin_plunder, I opened too many EE windows, and did not refresh 'em... hence I did not see your comments and posted mine instead... sorry! :)
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 5

Author Comment

by:Gwena
ID: 6462810
Hi :-)
  Yup.. I know how to save a file to the same dir as the executing program... what I'd like to know is how to set the default dir back to the dir of the exe... just as though the opendialog had never changed it at all... so that the code
MyStream.SaveToFile('test.txt');
Will save test.exe in the same dir as the exe...even after opendialog has executed.

I'd like to do what this code looks like it could do (but can't)

current.directory := ExtractFilePath(Application.ExeName);
0
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6462847
Perhaps this is what you are looking for.

ChDir(ExtractFilePath(Application.ExeName));
0
 
LVL 44

Accepted Solution

by:
CrazyOne earned 150 total points
ID: 6462851
Or perhaps this

SetCurrentDir(ExtractFilePath(Application.ExeName));

Delhi Help says

Sets the current directory.

Unit

Sysutils

Category

file management routines

function SetCurrentDir(const Dir: string): Boolean;

Description

The SetCurrentDir function sets the current directory. The return value is True if the current directory was successfully changed, or False if an error occurred.
0
 
LVL 5

Author Comment

by:Gwena
ID: 6462973
Thanks CrazyOne :-)
  That works perfectly! exactly what I was looking for :-)

gwen..
0
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6462979
You are welcome qwena :>)
0

Featured Post

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!

Question has a verified solution.

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

Suggested Solutions

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…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

749 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