chdir in parent shell: win32

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
: 7bit

I have a perl script which does a chdir() at the end.  When I call this
script (from windows console window, cmd.exe), I would like this dir to
be the new working directory for the console.


*.cmd/*.com/*.bat files which 'cd' do this.


I have tried using chdir(), system(cd <path>), exec(cd <path>).  But
none of them are able to change the directory of the parent shell.  I
even tried creating a *.cmd script which just did a cd, then exec()'d it
from the perl script, but this did not work either.


Is there a way to  do this?



Re: chdir in parent shell: win32

Quoted text here. Click to load it

You should consult the FAQ before posting:

perldoc -q env

I know the FAQ mentions Unix, but the principle is the same. Your Perl
script is running in a different process than the shell from which it
was invoked.

Quoted text here. Click to load it

Sure, they run in the same process as the shell that launched them.

Quoted text here. Click to load it

Please do not post HTML.

(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:

Re: chdir in parent shell: win32

Quoted text here. Click to load it

Basically, it can't be done like that - not in Windows, not in Unix, but
I can't say for sure that that's the case in other OSes.

Your perl program is a new process, and its parent - the cmd.exe window
from which you started it by typing "perl C:\wherever\" - doesn't
take much notice of the child. Specifically, changing the working
directory (or %ENV, for example) in the child has no effect on the

When you run a batch script it's read and executed by the cmd.exe you're
looking at, not by a child cmd.exe process. That's why your cmd.exe
responds to 'cd' commands in the batch script - it's reading and acting
on the .bat file, not forking off a copy of cmd.exe

You could try wrapping the perl script in a batch file - see pl2bat -
and manually add the chdir at the end. Or maybe there's some way of
sending a "change directory" message/event to the cmd.exe process itself
(with Win32::something)? Or you could even get perl to change to the
right directory and then it could start another copy of cmd.exe probably
using exec. I'm sure other people will think of even more answers :-)

Compare the situation on Unix if you had this script:

cd /

and you invoked it the following ways (assume your shell is bash):

bash ./foo
.. ./foo


pkent 77 at yahoo dot, er... what's the last bit, oh yes, com
Remove the tea to reply

Site Timeline