Beefy Boxes and Bandwidth Generously Provided by pair Networks kudra
go ahead... be a heretic
 
PerlMonks  

Proc::Background does not take file path with spaces

by dinasour (Novice)
on Apr 27, 2012 at 04:33 UTC ( #967515=perlquestion: print w/ replies, xml ) Need Help??
dinasour has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to use Proc::Background to start a .exe in background. The exe is located in C:\Program Files (x86)\<sompath>.exe Proc::Background fails..
my $Command = "C:\\Program Files (x86)\\vs.exe"; my $Id = Proc::Background->new($Command);
Error: backGround.pl: no executable program located at C:\Program
ALSO TRIED
my $CommandNew = '"C:\\Program Files (x86)\\vs.exe"'; my $ID = Proc::Background->new($CommandNew)
Error: Test.pl: cannot find absolute location of "C:\Program Is there a way around this problem ? Thanks for your time

Comment on Proc::Background does not take file path with spaces
Select or Download Code
Re: Proc::Background does not take file path with spaces
by chromatic (Archbishop) on Apr 27, 2012 at 05:19 UTC

    What happens when you escape the space?

    my $Command = "C:\\Program Files\ (x86)\\vs.exe";

    Improve your skills with Modern Perl: the free book.

      The Windows command shell (cmd.exe) expects whitespace in arguments to be properly quoted, and "properly quoted" in its terms means to use double quotes to surround the file name.

      As double quotes treat the backslash special, single-backslashing a space character doesn't make much sense to me. I would try

      my $Command = q{"C:\Program Files (x86)\vs.exe"};

      or the double-quoted variant

      my $Command = qq{"C:\\Program Files (x86)\\vs.exe"};

        Corion is correct, this is the way to do it. I usually write a quick sub that will process command line arguments for me:

        sub argify { while (my $aref = shift) { @$aref = map "/"$_/"", @$aref; } }
        I tried the folllowing
        my $CommandNew = qq("C:\\Program Files (x86)\\vs.exe"); print "Command is $CommandNew\n"; my $ID = Proc::Background->new($CommandNew);

        ERROR: Test.pl: cannot find absolute location of "C:\Program

        Also tried
        my $CommandNew = q(C:\\Program Files (x86)\\vs.exe); print "Command is $CommandNew\n"; my $ID = Proc::Background->new($CommandNew);

        ERROR: Test.pl: no executable program located at C:\Program
        I am still stuck
        I tried the folllowing
        my $CommandNew = qq("C:\\Program Files (x86)\\vs.exe"); print "Command is $CommandNew\n"; my $ID = Proc::Background->new($CommandNew);

        ERROR: Test.pl: cannot find absolute location of "C:\Program

        Also tried
        my $CommandNew = q(C:\\Program Files (x86)\\vs.exe); print "Command is $CommandNew\n"; my $ID = Proc::Background->new($CommandNew);

        ERROR: Test.pl: no executable program located at C:\Program
        I am still stuck
Re: Proc::Background does not take file path with spaces
by BrowserUk (Pope) on Apr 27, 2012 at 10:48 UTC

    Looking inside the Win32 component of Proc::Background it does some pretty dubious manipulations with the program component of its arg(s), including if, -- after it has done some other incomprehensible manipulations -- the length of the command argument is 0, then it wraps that zero length arg in quotes:

    if (length($arg) == 0 or $arg =~ /\s/) { $arg = "\"$arg\""; }

    None of which bodes well for success.

    If all you need is to start a process in the background, a simple:

    my $pid = system 1, 'c:/program Files (x86)/vs.exe';

    Does the job.

    if( kill $pid, 0 ) { ... will tell you if it is still running.

    kill $pid, 3; will terminate it.

    waitpid $pid,0; my $retcode = $?; will get the return code.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      On a windows command line, your parameters are all (white?)space separated. If you want a zero-length parameter you need to double quote it so it isn't ignored, like so:

      USAGE: mangle.exe [filename] [password] [index number] mangle.exe unprotected.dat "" 42

      Parameters that are all whitespace have the same problem and solution.

      No idea on why it had to be done that way though.

        If you want a zero-length parameter

        Hm. Okay, you got me there. Mind you, in 30 years I don't remember ever having used a command that took a "zero length parameter".

        Every command I'm aware of that takes a password has an option to supply it. And optional parameters always come at the end.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

Re: Proc::Background does not take file path with spaces
by Anonymous Monk on Aug 30, 2013 at 18:07 UTC
    Hey Guys,

    **** VICTORY ****

    I know this is old but I was trying to do the exact same thing on Windows XP running
    Strawberry Perl 5.8.18, and also using the Proc::Background Module. And I was literally
    on the verge of ripping my hair out one strand at a time..! Seriously!

    But I was FINALLY able to get Proc::Background to work using Absolute Paths that contain
    whitespace..!

    What I had to do was, first define the full path, then convert the Full Path to Windows Short
    Path to remove the whitespace. Lastly, include "perl" before the script name you want to execute
    in Proc::Background->new() Function... Like so:
    ### Full Path to Background Script: my $FULL_PATH = "C:\\Program Files\\Script_Dir\\my_script.pl"; ### Converts the FULL_PATH to a Win32 "Short Pathname" my $short_path = Win32::GetShortPathName($FULL_PATH); ### Prepend "perl " to before your script's path: # *my Array '@args' is just a space separated list of arguments my $proc = Proc::Background->new("perl $short_path @args"); ### Check if the Job is still alive... 1==YES, 0==NO my $alive = $proc->alive; ### While $alive is NOT '0', then it's still running... while ($alive ne 0) { $alive = $proc->alive; if ($alive ne 0) { print "PROCESS IS STILL RUNNING...\n"; } else { print "PROCESS IS NO LONGER RUNNING...\n"; } sleep 1; }
    Without pre-pending "perl" to the $short_path variable you would get the following error
    even though it could actually find the Path this time instead of saying its not there for "C:\Program"
    and stopping at the first occurrence of whitespace. So you would see this without including "perl":
    > no executable program located at C:\PROGRA~1\SCRIPT~1\MY_SC~2.PL

    And after I added "perl" to the Proc::Background command I can now execute my script in the background
    and watch it's progress...

    So I hope this helps someone out there, cause this was the first link that would show up in Google
    no matter what search terms I used for describing this problem...


    So good luck gents...!


    Thanks,
    Matt

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2014-04-20 04:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls