Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Changing directory in cmd window

by roho (Chancellor)
on Sep 28, 2009 at 16:08 UTC ( #797954=perlquestion: print w/replies, xml ) Need Help??

roho has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to change directories in a cmd window on MS Windows using Perl. I've tried using chdir("\\newdir") as well as system("cd \\newdir"), but neither leaves me in the new directory after my Perl program ends. These statements change the directory internally for my Perl program but they do NOT externally change the directory in the cmd window. Does anyone know of a way to do this? TIA

Roy Fulbright

"Its not how hard you work, its how much you get done."

Replies are listed 'Best First'.
Re: Changing directory in cmd window
by ikegami (Pope) on Sep 28, 2009 at 16:45 UTC

    Each process has its own work directory. Changing the work directory of one of its children (chdir("\\newdir")) or one of its grandchildren (system("cd \\newdir")) will have no effect on the work directory of the shell.

    One solution it to output a directive to the shell and have the shell interpret that directive:

    >cd C:\ >for /f usebackq %q in (`perl -e"print 'foo'"`) do cd %q >cd C:\foo

    (Change %q to %%q in a batch file)

Re: Changing directory in cmd window
by FalseVinylShrub (Chaplain) on Sep 28, 2009 at 16:29 UTC

    I'm pretty sure the answer is "you can't" change directory and have it remain in effect after your script ends.

    Depending on why you're trying to do that, perhaps there is a way to spawn a command prompt from inside your perl process, thus keeping the context. I'm not an expert on Windows so someone else will have to tell you the best way to do that - looks like someone has already.

    Hope this helps.

Re: Changing directory in cmd window
by roho (Chancellor) on Sep 28, 2009 at 19:55 UTC
    Thanks to all for your input. Meanwhile I have continued looking for a solution and have found one.
    I invoke the Perl program from a batch file and use '&' to run another batch file generated by the Perl program.

    cda.bat

    @echo off cda.pl %* & \bat\aa.bat

    cda.pl

    #!/usr/bin/perl ###################################################################### # Name: cda.pl # Desc: CD to alternate directory (optionally to sub-directory). ###################################################################### use strict; use warnings; my $dir = "\\newdir"; if (@ARGV) { $dir .= "\\subdir" . rj(2,'0',shift); } my $ofile = "\\bat\\aa.bat"; open(OFILE, '>', $ofile) or die "Error opening $ofile: $!\n"; print OFILE "cd $dir\n"; print OFILE "cls\n"; print OFILE "dir /p /ogen\n"; close OFILE; exit; ################################################################### # This subroutine adds commas and right-justifies input number. ################################################################### sub rj { my $pad = shift; my $dlm = shift; local($_)=@_; 1 while s/(.*\d)(\d\d\d)/$1,$2/; $_ = ${dlm}x($pad-length($_)).$_; }

    "Its not how hard you work, its how much you get done."

Re: Changing directory in cmd window
by ahmad (Hermit) on Sep 28, 2009 at 16:25 UTC

    Hello

    Something like this might work ??

    system 'cmd /K "cd c:\"';

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://797954]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2020-02-24 00:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (104 votes). Check out past polls.

    Notices?