Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Using System command

by 9mohit2 (Sexton)
on Oct 04, 2016 at 06:17 UTC ( [id://1173212]=perlquestion: print w/replies, xml ) Need Help??

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

Hi I want to run pdftotext utillity using perl but I am not able to do so. I am using system command to fistly change directory to target directory.Then I want to execute command like pdftotext -layout R1.pdf.

Replies are listed 'Best First'.
Re: Using System command
by 9mohit2 (Sexton) on Oct 04, 2016 at 06:22 UTC
    Currently I am using
    my $cmd = "cd C:\\ABC\\"; $cmd .= "pdftotext -layout R1.pdf"; system($cmd);

    And the Error i am getting is ---System cannot get the specifies path.

      I suspect that if you print your $cmd you'll see why:

      cd C:\\ABC\\pdftotext -layout R1.pdf

      Probably is not what you want to execute.

      Perl has a builtin to change directory: chdir

      In the command line, two commands have to been concatenated with & or && (NB: the latter && only execute the second command if the previous was succesfull ms odcs)

      See a more userfriendly explaination at ss64.com for details.

      L*

      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

        Tried the below code

        my $cmd = "cd C:\\ABC\\"; my $cmd1 = "pdftotext -layout R1.pdf"; system($cmd&&$cmd1);

        But the error this time is "'pdftotext' is not recognized as an internal or external command, operable program or batch file"

      Hello 9mohit2,

      If you want to run two system commands like this, you either need to run them separately:

      system('cd C:\\ABC\\'); system('pdftotext -layout R1.pdf');

      or else combine them using a syntax recognised by the command prompt. For example, this works for me on Windows:

      perl -wE "system('cd .. & dir');" # Note: ^

      But there’s no need to use system to change the current working directory; use Perl’s built-in chdir function.

      Hope that helps,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

        > run them separately

        Would it help in this case? The cd in the first system wouldn't change the current directory for the script, only for the fork, so the next system would run in the previous directory. Or is cd global on Windows?

        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-04-19 20:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found