Recent

Author Topic: [solved by compromise] Cannot change directory permanently with ChDir()  (Read 10460 times)

Hartmut

  • Hero Member
  • *****
  • Posts: 751
My program shall at the end change the current drive and directory of the DOS box permanently. I use the command ChDir() for that, but it does not work (nothing happens):

Code: Pascal  [Select][+][-]
  1. {$MODE TP} // changing to Delphi does not help
  2. begin
  3. {my program...}
  4. ChDir('d:\fpc');
  5. end.

It looks like that the current drive and directory are only changed temporarily and are restored when the program finishes. I must avoid this restore. I use FPC 2.6.4 on Win7 32 bit. Can somebody help please?

Info: when I compile this program with very old Turbo Pascal, then current drive and directory of the DOS box are changed permanently, so this seems to be incompatible between Turbo Pascal and FPC.
« Last Edit: November 29, 2015, 06:45:54 pm by Hartmut »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11459
  • FPC developer.
Re: Cannot change directory permanently with ChDir()
« Reply #1 on: November 28, 2015, 11:56:41 am »
No it is simply Dos vs Windows.

Windows can run multiple applications, so each has its own sets of working directories.

On Dos, the working dir is global, and iirc needs to explicitely saved before shelling etc.

Hartmut

  • Hero Member
  • *****
  • Posts: 751
Re: Cannot change directory permanently with ChDir()
« Reply #2 on: November 28, 2015, 12:06:31 pm »
Hello marcov,

I don't understand your answer complete. My program shall run in a DOS box and shall change the current DOS box directory of a drive when it finishes.

What means "iirc needs to explicitely saved..."? What is iirc? What must I do before my program finishes? Thanks in advance.
« Last Edit: November 28, 2015, 12:37:27 pm by Hartmut »

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: Cannot change directory permanently with ChDir()
« Reply #3 on: November 28, 2015, 12:07:47 pm »
You are using Win7 - how can you run such a program under DOS? I think you have to cross-compile.

Hartmut

  • Hero Member
  • *****
  • Posts: 751
Re: Cannot change directory permanently with ChDir()
« Reply #4 on: November 28, 2015, 12:11:44 pm »
Hello wp,

my program shall run in a DOS box of Win7 and shall change the current DOS directory when it finishes and returns to the DOS box.
When I compile the program under old Turbo Pascal, ist works, but not under FPC.

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: Cannot change directory permanently with ChDir()
« Reply #5 on: November 28, 2015, 12:16:04 pm »
I'm not a specialist here, but I guess that your program, even if started from a cmd window, creates a new process with its own current dir, environment etc. When this program ends it restores the old settings.

Why don't you put your program into a batch file which has a "cd" at the end?

Hartmut

  • Hero Member
  • *****
  • Posts: 751
Re: Cannot change directory permanently with ChDir()
« Reply #6 on: November 28, 2015, 12:19:00 pm »
Hello wp,

because the new directory to set is computed by the program and a batch file would be static.

balazsszekely

  • Guest
Re: Cannot change directory permanently with ChDir()
« Reply #7 on: November 28, 2015, 12:43:18 pm »
@Hartmut

I did not program in Turbo Pascal a very long time ago and to be perfectly honest I cannot see why you want to code in TP(2015) other then nostalgic reason of course. 8-)
Anyway back to your problem...If I remember correctly you can do something like this in TP, maybe it's also true for fpc:
Code: Pascal  [Select][+][-]
  1. uses dos;
  2. begin
  3.    Exec (GetEnv('COMSPEC'),'cd c:\')
  4. end.
Basically you call cmd(command in dos days) and let the os change the dir.

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: Cannot change directory permanently with ChDir()
« Reply #8 on: November 28, 2015, 12:54:19 pm »
You should explain why it is important to be in a different folder/drive after your program has finished. Because you want to run another application there? You can do this in your program by calling "RunCommand", "ExecuteProcess, etc - see http://wiki.lazarus.freepascal.org/Executing_External_Programs.

Hartmut

  • Hero Member
  • *****
  • Posts: 751
Re: Cannot change directory permanently with ChDir()
« Reply #9 on: November 28, 2015, 01:11:17 pm »
Hello GetMem,

I tried your program but sorry, it did not work: the new directory was only set temporarily and was restored to the old value, when the program terminates. This automatic restore I must avoid. Which part of FPC does this? A part of unit system? Can somebody help?

Hello wp,

the purpose of the program is to easily change the current directory of the DOS box, like old "Norton change directory" (ncd.exe) did. This workes in old Turbo Pascal, but does not work in FPC, so they are not compatible at this point. I must avoid the automatic restore of the former current directory when the program terminates.

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: Cannot change directory permanently with ChDir()
« Reply #10 on: November 28, 2015, 01:38:14 pm »
I am almost 100% sure that this is not a limitation of fpc, but a well-thought restriction of the multi-user/multi-process operating system. What if you start two programs in your command window (please rember: this is not DOS any more!) and one changes the directory while the other one relies on being in the old directory?

But I am also sure that there are ways to achieve what you want. You should look in the Windows documentation on how to ask a parent process to change its directory.

Don't blame FPC for the changes having occured during the last 30 or so years. The documentation at least is very specific on "ChDir": "Chdir changes the working directory of the process to S."

balazsszekely

  • Guest
Re: Cannot change directory permanently with ChDir()
« Reply #11 on: November 28, 2015, 01:53:16 pm »
Ok. I did run a few tests(VM-->Win98--> Dos Mode). ChDir works fine,  the FPC(ide) is changing back the directory not the executable itself. Build your exe with fpc, then run from command line(see attachment).

PS: It's true the after you close Turbo Pacal, the directory is not changed back, but that's the feature of the IDE(maybe drawback?), I don't know.

Hartmut

  • Hero Member
  • *****
  • Posts: 751
Re: Cannot change directory permanently with ChDir()
« Reply #12 on: November 28, 2015, 02:04:18 pm »
Hello wp,

when I compile the same program with old Turbo Pascal, it works. So the problem can not be Windows.
If "ChDir" is to specific in FPC, is there another command I can use for that?

Hello GetMem,

I don't start my program by the IDE of FPC. I open a DOS box and start my compiled EXE. So the EXE changes the directory back when it terminates. How can I avoid this?

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Cannot change directory permanently with ChDir()
« Reply #13 on: November 28, 2015, 02:05:47 pm »
Ok. I did run a few tests(VM-->Win98--> Dos Mode). ChDir works fine,  the FPC(ide) is changing back the directory not the executable itself. Build your exe with fpc, then run from command line(see attachment).

Dis the same in Win7 in a console (Start->cmd): the directory reverts back to the starting one.

To me this indicates that is is the actua OS that does this, not the program.

Bart

Hartmut

  • Hero Member
  • *****
  • Posts: 751
Re: Cannot change directory permanently with ChDir()
« Reply #14 on: November 28, 2015, 02:08:19 pm »
Hello Bart,

when the EXE was compiled by old Turbo Pascal, it works. When I compile the same source with FPC, it works not. So for me it must be a problem of the EXE and not the OS.
« Last Edit: November 28, 2015, 02:19:30 pm by Hartmut »

 

TinyPortal © 2005-2018