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

Having an issue invoking excel with 1 file for an argument. It seems to choke at the dir name due to spaces:
"'C:\Program' is not recognized as an internal or external command, operable program or batch file."

I am having the issue with system(), but I also had it with Win32::Process::Create, I've tried single quotes also but I cannot seem to get around it. Here's a snippet of the code:
my $app = 'C:\Program Files\Microsoft Office\Office12\excel.exe'; my $arg = "C:\\Documents and Settings\\my.dir\\My Documents\\My Compan +y\\QA\\$newFilename"; #Win32::Process::Create( $Win32processObj,$app,$arg,0,NORMAL_PRIORITY_ +CLASS,"." ) # or die "Aborting... creating process failed due to $!\n"; system("$app $arg");
Thank you in advance!

Replies are listed 'Best First'.
Re: Issues with dir names containing spaces-
by ikegami (Patriarch) on Jul 21, 2009 at 17:38 UTC

    The third arg of Win32::Process::Create is a command line, not an argument for the program. See CreateProcess for details.

    However, it's much easier to use the multiple argument form of system rather than quoting:

    my $app = 'C:\\Program Files\\Microsoft Office\\Office12\\excel.exe'; my $arg = "C:\\Documents and Settings\\my.dir\\My Documents\\My Compan +y\\QA\\$newFilename"; system($app, $arg);
      Thank you all for the replies. Will definitely give it a
      shot as soon as possible. FYI, I do use strict and
      warning, but all I had posted was a snippet.
      Thanks again!

        Hello Monks,
        well, I am still having issues with this script I am trying to write. The double quotes following the 'QA' in the output I believe is responsible for not allowing excel to find the dynamically created file thus generating a file not found error... any suggestions on the matter? Here is a snippet of the script:

        my $app = '"C:\Program Files\Microsoft Office\Office12\excel.exe"'; my $arg = '"C:\Documents and Settings\my.name\My Documents\My Company\ +QA\"' . $newFilename; #system("$app $arg"); print $app . "\n"; print $arg;

        My output = "C:\Program Files\Microsoft Office\Office12\excel.exe" "C:\Documents and Settings\my.name\My Documents\My Company\QA\"TestingWorksheet_07232009.xlsx

Re: Issues with dir names containing spaces-
by jrsimmon (Hermit) on Jul 21, 2009 at 17:10 UTC

    To use a path which contains spaces in a win32 command window, you have to surround the path in quotes. Otherwise the interpreter sees the first space delimited text as the command and each following delimited text as arguments to that command.

    use strict; use warnings; my $app = '"C:\Program Files\Microsoft Office\Office\excel.exe"'; my $arg = '"C:\Documents and Settings\Administrator\My Documents\data\ +Excel\temp.xls"'; system("$app $arg");

    Also, please remember to

    use strict; use warnings;

    Update:
    You'll need to do the same thing with your args. Updated code with a tested and working mini-script.

Re: Issues with dir names containing spaces-
by kennethk (Abbot) on Jul 21, 2009 at 16:58 UTC
    I think the issue is that files names that contain spaces must be wrapped in quotes on MS systems. Have you tried:

    my $app = 'C:\"Program Files"\"Microsoft Office"\Office12\excel.exe'; my $arg = "C:\\\"Documents and Settings\"\\my.dir\\\"My Documents\"\\\ +"My Company\"\\QA\\$newFilename"; #Win32::Process::Create( $Win32processObj,$app,$arg,0,NORMAL_PRIORITY_ +CLASS,"." ) # or die "Aborting... creating process failed due to $!\n"; system("$app $arg");

    Incidentally, if you'd like to avoid some of the excessive use of escaping characters, take a look at quotemeta and/or Quote and Quote like Operators (\Q and \E).

Re: Issues with dir names containing spaces-
by Anonymous Monk on Jul 21, 2009 at 17:00 UTC
    try quoting like in the cmd:
    my $app = '\"C:\Program Files\Microsoft Office\Office12\excel.exe\"'; print $app # "C:\Program Files\Microsoft Office\Office12\excel.exe"