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

Dear Monks,

I want to open a new cmd prompt on Windows the list the directory content. But the dir cmd result appeared on the old cmd windows. How can i switch to use new windows? Many thanks in advance!

#!/usr/bin/perl use warnings; use strict; use diagnostics; system ("start \"New prompt!\" cmd.exe |dir");

Replies are listed 'Best First'.
Re: Open new cmd prompt and list directory content on it
by GotToBTru (Prior) on Jun 22, 2015 at 17:12 UTC

    System does a fork to execute the command, so yes, you get the output in your current window. If you need a separate window to stay open, you will want to use one of the Win32 modules on CPAN. Check out Win32::GUI::Tutorial.

    The addition of the start command does open a new window, and transfers the focus to it. On my computer, it does not execute the dir command. But still does not help with your problem. Once having done the dir command, what next? I think that will dictate whether or not you can handle this with a properly formatted commandline or need to interact with the window in a way that the Win32 modules will allow.

    Dum Spiro Spero

      GotToBTru : System does a fork to execute the command, so yes, you get the output in your current window. If you need a separate window to stay open, you will want to use one of the Win32 modules on CPAN. Check out Win32::GUI::Tutorial.

      No

Re: Open new cmd prompt and list directory content on it
by VinsWorldcom (Prior) on Jun 22, 2015 at 18:16 UTC

    Your "|" should be a "/k".

Re: Open new cmd prompt and list directory content on it
by GotToBTru (Prior) on Jun 22, 2015 at 18:35 UTC

    The addition of the start command does open a new window, and transfers the focus to it. On my computer, it does not execute the dir command. And still this does not help with your problem. Once having done the dir command, what do you want to do next? I think that will dictate whether or not you can handle this with a properly formatted commandline or need to interact with the window in a way that the Win32 modules will allow. Do you just need a list of files? There are simpler ways to get that without using system.

    Dum Spiro Spero
Re: Open new cmd prompt and list directory content on it
by marinersk (Priest) on Jun 22, 2015 at 23:46 UTC

    LOL

    Summarizing all the above:

    • system ("start", "\"New prompt!\"", "cmd.exe", "/k", "dir");
    • Recommend you look into the /Doption for the STARTcommand.

      Thank you so much! /k works for me now.

Re: Open new cmd prompt and list directory content on it
by Anonymous Monk on Jun 22, 2015 at 17:32 UTC
    When you copy/paste that command in cmd.exe, it behaves the exact same way -- thats what pipes do, they execute in the current window

    remove the pipe character and read "help cmd.exe"

      Or, for later versions of Windows, use

      C:\> cmd.exe /?

      This has the advantage over this suggestion in that it also informs how to execute more than one command.

      Dum Spiro Spero