in reply to Re^3: Perl calling command prompt?
in thread Perl calling command prompt?

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re^5: Perl calling command prompt?
by Firefly258 (Beadle) on Nov 27, 2006 at 17:16 UTC
    Why don't you just invoke the batch file via it's fully-qualified-path-name like this?
    system("cmd /c \"E:\\MyPerl\\Trial_Scripts\\batch_file.bat\"");

    or if you still wish to change directories,
    system("cd E:\\MyPerl\\Trial_Scripts && batch_file.bat");

    Just don't count on system("cd ...") changing the current directory over the lifetime of the perl script, It only changes directories for that particular instance of system() (or rather, changes directories for the shell that is invoked via system()). As soon as system() returns, perl continues operating in the directory it was in before system() was called.

    Or if you wish for a persistent directory change, use perl's own chdir().

    update: just cleaned up some nasty formatting error


    perl -e '$,=$",$_=(split/\W/,$^X)[y[eval]]]+--$_],print+just,another,split,hack'er
Re^5: Perl calling command prompt?
by SheridanCat (Pilgrim) on Nov 27, 2006 at 17:20 UTC
    Aha, the real question is asked.

    Just do this:

    system( "c:/path_to_batfile/mybat.bat" );
    No need to call cmd.exe or the like. The shell knows what you want to do with a .bat file.
Re^5: Perl calling command prompt?
by pKai (Priest) on Nov 27, 2006 at 17:25 UTC

    D:\temp>echo echo Batch! > batch.cmd D:\temp>cd.. D:\>c: C:\WINNT>perl -e "system q(cd /d d:\\temp & batch.cmd)" D:\temp>echo Batch! Batch! C:\WINNT>

    • Use cd /d to to be on the safe side and switch the drive too.
    • The cd only applies to the cmd opened by that system (as mentioned by others in this thread.)
    • You can also use && instead of the single & for conditional command chaining (think &&/and in Perl.)

    Hope that helps

    Update: lagging behind... Firefly258 was faster