Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

'C:\Program' is not recognized as an internal or external command, operable program or batch file.

by prab2112 (Novice)
on Nov 18, 2014 at 06:47 UTC ( [id://1107536]=perlquestion: print w/replies, xml ) Need Help??

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

When i try to run the code below in Windows i get this error "'C:\Program' is not recognized as an internal or external command, operable program or batch file." What is wrong with my code?
use Win32::ChangeNotify; $Path='C:\Users\user\Downloads'; #$Events='FILE_NAME'; $browser="C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome +.exe"; print $browser; #$changes=0; #$notify = Win32::ChangeNotify->new($Path,$WatchSubTree,$Events); print "- ",scalar(localtime), " Launching $browser...\n"; system("$browser $Path");
  • Comment on 'C:\Program' is not recognized as an internal or external command, operable program or batch file.
  • Download Code

Replies are listed 'Best First'.
Re: 'C:\Program' is not recognized as an internal or external command, operable program or batch file.
by Loops (Curate) on Nov 18, 2014 at 07:03 UTC

    Not very up on Windows, but it seems that system is passing the request to Windows where the space separated elements of the path are being interpreted as individual parameters. If you look at the documentation for system, it suggests the following would work:

    system($browser, $Path);
Re: 'C:\Program' is not recognized as an internal or external command, operable program or batch file.
by Anonymous Monk on Nov 18, 2014 at 07:05 UTC
    system explains about when it invokes the shell ... you want to avoid the shell by using multiple arguments to syste as Loops demonstrates
Re: 'C:\Program' is not recognized as an internal or external command, operable program or batch file.
by ww (Archbishop) on Nov 18, 2014 at 15:47 UTC

    The elephant that's been mentioned only implicitly is 'doze-style quoting.'

    Sorry, this illustration (which presents two ways of working with Win32) is a bit on the long side but here goes, anyway...

    #!/usr/bin/perl use strict; use warnings; use 5.018; # #1107536 my $program="D:\\GnuWin32\\bin\\ls.exe"; print "$program\n"; runit ($program); sub runit { system ("$program"); if ($? == -1) { print "failed to execute: $!\n"; } say "\n\t DEBUG: reached the end of sub runit at Ln23"; sleep 1; } say "\n Done with first sub, back in main. \n"; my $path = 'D:/GnuWin32/bin/'; my $pgm="ls.exe"; my $cmd ="$path" . "$pgm"; runit2 ($cmd); sub runit2 { say "\n\tEntering runit2\n"; sleep 3; my ($cmd_in_sub) = @_; say "\n\t DEBUG: $cmd_in_sub\n"; system ("$cmd_in_sub"); if ($?) { print "failed to execute: $!\n"; } } say "\n Done with second sub ";

    Execution:

    D:\>D:\_Perl_\PMonks\1107536NON-GUI.pl d:\_wo D:\GnuWin32\bin\ls.exe .. TestFiles Connexions tbos.css CPM-ZCPR ViennaProtocol.txt created_psp_files WinSHAetc DEBUG: reached the end of sub runit at Ln23 Done with first sub, back in main. Entering runit2 DEBUG: D:/GnuWin32/bin/ls.exe ... TestFiles Connexions tbos.css CPM-ZCPR ViennaProtocol.txt created_psp_files WinSHAetc.... Done with second sub D:\>

    M$Win mistreats (file and/or dir)_names_with_spaces unless the whole Path/Program_name match is double-quoted.

    NB: This also works to invoke a Windows GUI program, such as Chrome or Beyond Compare.

    Updated: by removing extraneous code; adding note

Re: 'C:\Program' is not recognized as an internal or external command, operable program or batch file.
by MidLifeXis (Monsignor) on Nov 18, 2014 at 13:50 UTC

    In addition to the multi-argument system call, you could instead quote the browser.

    system( qq("$browser" "$Path") );

    --MidLifeXis

Re: 'C:\Program' is not recognized as an internal or external command, operable program or batch file.
by aitap (Curate) on Nov 18, 2014 at 20:26 UTC

    Wherever available, you should use LIST-style forms of command launching functions. For example, prefer system("rm", "-rf", @temp_directories) over system("rm -rf @unneeded"). The same goes for pipe-open, exec and so on.

    This is because when Perl encounters a command which consists of a single string containing so called special characters it passes the whole string to the shell. And modern filesystems allow spaces in file names which are not getting escaped, and this is where your command breaks. Some filesystems even allow glob characters (like * [ ]). Even more fun: imagine creating a file called -rf ~ and trying to remove it by system("rm $filename").

    Bad news: list form of open is not supported on Windows. Good news: list form of open2 is. And so is high-level IPC::Run (but I got some problems with binary files transferred via pipe when I used this module).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-04-26 05:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found