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

I am trying to execute program cjpeg on a Windows platform. The directory names are not working with this command because of the spaces within the names. Here is the command that isn't working: system("cjpeg.exe -gr /program Files/Lego Mindstroms/Image0001.bmp"); and I am receiving the following error.... Bad command of file name. Also, I tried the qx() function but I am not sure how it works. Any suggestions would be appreciated.

Replies are listed 'Best First'.
Re: execute a file
by stephen (Priest) on Apr 13, 2001 at 04:35 UTC
    It's quite possible that 'cjpeg.exe' isn't in your Path. To avoid problems like this, specify the full path to cjpeg.exe, like so:
    system("C:\\WINNT\\System32\\freecell.exe") == 0 or die "Returned $?";
    (Example with freecell because I don't know where cjpeg is and don't want to give misinformation)

    stephen

Re: execute a file
by cLive ;-) (Prior) on Apr 13, 2001 at 04:29 UTC
    You need to quote the filename. I can think of two ways:
    # split your arguments up (recommended) system("cjpeg.exe","-gr","/program Files/Lego Mindstroms/Image0001.bmp +"); # single quote the filepath system("cjpeg.exe -gr '/program Files/Lego Mindstroms/Image0001.bmp' " +);

    Read the docs here.

    cLive ;-)

    Update: - see Stephen's reply below. Quick note, he uses \\ in the file path because \ in a double quoted string has a special meaning and must be 'escaped'.

    If you want, you can put the actual path in a single quoted string, ie:

    system('C:\path\to\cjpeg.exe','-gr','C:\program Files\Lego Mindstroms\ +Image0001.bmp');

    or write it in non-windows format:

    system('C:/path/to/cjpeg.exe','-gr','C:/program Files/Lego Mindstroms/ +Image0001.bmp');

    For more information, look up interpolation in the super search.

    Another quick note, Perl seems to be rather nice as far as your directory delimiter is concerned, allowing you to use / or \ on windoze machines (AFAIK). Just remember though that \ can have special meaning.

    Finally, "Mindstroms" or "Mindstorms"??? :)

      Thanks for your suggestions. I tried both of your recommendations and I am still receiving the error. Can you think of something else?
Re: execute a file
by eejack (Hermit) on Apr 13, 2001 at 08:37 UTC
    It is possible that the error is not a perl error, but a windows or cjpeg error. Windows is rather funny about how it treats spaces and more than eight characters in the path.

    Perhaps you need to *dosify* the path like \PROGRA~1\LEGOMI~1\IMAGE0~1.BMP

    I would try it on your command prompt first, and then work out exactly what windows likes - then work out which is the best way to quote it properly in your perl. EEjack

      There may be a simpler way to "DOSify" the file name but this works for me. There's a Win32 API call GetShortPathName that returns the DOSified path name. You can call it from perl using Win32::API. The prototype is
      DWORD GetShortPathName( LPCTSTR lpszLongPath, // null-terminated path string LPTSTR lpszShortPath, // short form buffer DWORD cchBuffer // size of short form buffer );
      The equivalent call in perl can be written like this:
      use Win32::API; #import the function my $GetShortPathName = new Win32::API("kernel32","GetShortPathName",[ +P,P,N ],N); if (not defined $GetShortPathName) { die "could not import GetShortPathName"; } #the name we want to convert $longname = "C:\\Program Files\\Microsoft Office\\Office\\OLREAD9.TXT" +; #create a string big enough to hold the shortname $shortname = " " x 80; #retval will be length of the DOSified short name $retval = $GetShortPathName->Call($longname,$shortname,80); #trim the trailing blanks #recall that $shortname = " " x 80 #and retval is the length of the name returned by GetShortPathName $shortname = substr($shortname,0,$retval); print "longname = \n\"$longname\", \nshortname = \n\"$shortname\"";
      On my system, this returns
      C:\temp>perl shortname.pl longname = "C:\Program Files\Microsoft Office\Office\OLREAD9.TXT", shortname = "C:\PROGRA~1\MICROS~3\Office\OLREAD9.TXT" C:\temp>
Re: execute a file
by damian1301 (Curate) on Apr 13, 2001 at 04:06 UTC
    There is the exec function which executes a LIST through the first argument which is the program it should be executed though (exec(PRGM, LIST)). Note that exec will NEVER return anything on success or failure unlike system

    Almost a Perl hacker.
    Dave AKA damian

    I encourage you to email me