1

I am trying to use perl to run two different commands - both of which need to be executed in a separate command prompt.

Here are some of the details:

when I run

system('start cmd /k "cd c:\PerlExamples && perl Perl_Ex_1.pl"');

it works as expected - it opens a new command prompt, paths to the directory I need and runs my script.

However, I want to use this system... command in a loop where each time I am running a different example script. But when I run the following lines of code,

my $cmd = "perl Perl_Ex_1.pl";
system("start cmd /k cd c:\PerlExamples && $cmd");

it opens a new command prompt and paths to the directory I need. But it runs my example script in the original window and not in the newly opened command prompt.

Is there a way for me to do this?

Thank you.

4
  • Have you tried /c instead of /k? I cannot test right now (no Windows available). Or try without start. Commented Oct 4, 2016 at 19:44
  • The issue is that I need to keep that window open and not close it. Commented Oct 4, 2016 at 20:11
  • Could it be because you don't have quotes around ` cd c:\PerlExamples && $cmd` in the second command? Commented Oct 5, 2016 at 7:04
  • @user2243865 If I put quotes around that, it considers $cmd as part of string instead of as a variable. Commented Oct 5, 2016 at 16:30

1 Answer 1

1

Thanks to the wisdom of the perl monks, I found the solution to my problem.

Link to the solution: http://www.perlmonks.org/?node_id=1173278

The solution is to either use:

my $cmd = "perl Perl_Ex_1.pl";
system(qq{start cmd /k "cd c:\\PerlExamples && $cmd"});

or use:

my $cmd = "perl Perl_Ex_1.pl";
system qq[ start /D c:\\PerlExamples cmd /k  $cmd ];

Thank you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.