Executing an application whose directory path contains blank spaces

This is a discussion on Executing an application whose directory path contains blank spaces within the PHP Language forums, part of the PHP Programming Forums category; Hello, I can't find a way to execute a Windows application, whose directory path contains blank spaces, from a ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-20-2005
Jean-Marc Molina
 
Posts: n/a
Default Executing an application whose directory path contains blank spaces

Hello,

I can't find a way to execute a Windows application, whose directory path
contains blank spaces, from a PHP script. I also wonder if the problem
happens under Linux and other OS.

Working dir : "C:\test copy"

"copy.php" PHP script :
<?php

system ('"C:\test copy\mycopy" /B "C:\test copy\folder1\file1" "C:\test
copy\folder2\file2"');

>


mycopy.bat batch :
copy %1 %2 %3

Executign this script I get the following error message :
« C:\>php "test copy\copy.php
'C:\test' is not recognized as an internal or external command,
operable program or batch file. »

If I replace « test copy » by « test » or « test_copy », it works.

The problem doesn't happen if the command parameters are not double quoted :

<?php

system ('"C:\test copy\mycopy" /B');

>


Note that you can just copy & paste these commands and see how they work in
a DOS command window.

How should I transform these blank spaces characters ? For example in a URL
you replace blank spaces by « %20 »...

The general idea is to execute an application located in the « "C:\Program
Files" directory. On the <http://www.php.net/manual/en/function.system.php>
page of the PHP manual someone proposed a solution but its command line
doesn't involve double quoted parameters.

--
Jean-Marc.

Reply With Quote
  #2 (permalink)  
Old 01-20-2005
Alvaro G Vicario
 
Posts: n/a
Default Re: Executing an application whose directory path contains blank spaces

*** Jean-Marc Molina wrote/escribió (Thu, 20 Jan 2005 09:58:30 +0100):
> mycopy.bat batch :
> copy %1 %2 %3


You're missing quotes in the batch file?

copy "%1" "%2" "%3"

--
-- Álvaro G. Vicario - Burgos, Spain
-- Thank you for not e-mailing me your questions
--
Reply With Quote
  #3 (permalink)  
Old 01-20-2005
Paul Barfoot
 
Posts: n/a
Default Re: Executing an application whose directory path contains blank spaces

Jean-Marc

I have found two ways around this problem:

1. Accept that paths with spaces in are going to get treated as two
arguments and work around it - change line in php script to:

system ('"C:\test copy\mycopy" /B C:\test copy\folder1\file1 C:\test
copy\folder2\file2');

command %1 %2 %3
%4 %5

and then alter your batch file like this:

copy %1 "%2 %3" "%4 %5"

Note spaces between %2 & %3, and between %4 & %5

2. Create your batch file on the fly in the php script - I think this is
neater as you could pass source and destination filepaths in as variables:

<?
$handle=fopen("C:/test copy/mycopy.bat", "w");
$command = "copy /B \"C:\\test copy\\folder1\\file1\" \"C:\\test
copy\\folder1\\file2\"";
//echo "$command<br />\n"; //used for testing
fwrite($handle, $command);
system ("\"C:/test copy/mycopy.bat\""); //note I have escaped the " and used
forward slashes in the path
fclose($handle);
?>

Take your pick!

--
Paul Barfoot


"Jean-Marc Molina" <jmmolina@PASDEPOURRIEL-free.fr> wrote in message
news:41ef72b6$0$9969$636a15ce@news.free.fr...
> Hello,
>
> I can't find a way to execute a Windows application, whose directory path
> contains blank spaces, from a PHP script. I also wonder if the problem
> happens under Linux and other OS.
>
> Working dir : "C:\test copy"
>
> "copy.php" PHP script :
> <?php
>
> system ('"C:\test copy\mycopy" /B "C:\test copy\folder1\file1" "C:\test
> copy\folder2\file2"');
>
>>

>
> mycopy.bat batch :
> copy %1 %2 %3
>
> Executign this script I get the following error message :
> « C:\>php "test copy\copy.php
> 'C:\test' is not recognized as an internal or external command,
> operable program or batch file. »
>
> If I replace « test copy » by « test » or « test_copy », it works.
>
> The problem doesn't happen if the command parameters are not double quoted
> :
>
> <?php
>
> system ('"C:\test copy\mycopy" /B');
>
>>

>
> Note that you can just copy & paste these commands and see how they work
> in
> a DOS command window.
>
> How should I transform these blank spaces characters ? For example in a
> URL
> you replace blank spaces by « %20 »...
>
> The general idea is to execute an application located in the « "C:\Program
> Files" directory. On the
> <http://www.php.net/manual/en/function.system.php>
> page of the PHP manual someone proposed a solution but its command line
> doesn't involve double quoted parameters.
>
> --
> Jean-Marc.
>



Reply With Quote
  #4 (permalink)  
Old 01-20-2005
Jean-Marc Molina
 
Posts: n/a
Default Re: Executing an application whose directory path contains blank spaces

Alvaro G Vicario a écrit/wrote :
> You're missing quotes in the batch file?
>
> copy "%1" "%2" "%3"


No, my parameters are already quoted, I think it even double-double-quotes
them :).

Command line :

« C:\>php "C:\test copy\copy.php"
'C:\test' is not recognized as an internal or external command,
operable program or batch file. »

--
Jean-Marc.

Reply With Quote
  #5 (permalink)  
Old 01-20-2005
Jean-Marc Molina
 
Posts: n/a
Default Re: Executing an application whose directory path contains blank spaces

Paul Barfoot a écrit/wrote :
> $command = "copy /B \"C:\\test copy\\folder1\\file1\" \"C:\\test
> copy\\folder1\\file2\"";


Thanks but I supposed « copy » was located in a path containing blank
spaces, like "C:\Program Files" for example, that's why I used a "C:\test
copy" test folder, this was just an example.

--
Jean-Marc.

Reply With Quote
  #6 (permalink)  
Old 01-21-2005
Chung Leong
 
Posts: n/a
Default Re: Executing an application whose directory path contains blank spaces

"Jean-Marc Molina" <jmmolina@PASDEPOURRIEL-free.fr> wrote in message
news:41eff055$0$9975$636a15ce@news.free.fr...
> Alvaro G Vicario a écrit/wrote :
> > You're missing quotes in the batch file?
> >
> > copy "%1" "%2" "%3"

>
> No, my parameters are already quoted, I think it even double-double-quotes
> them :).
>
> Command line :
>
> « C:\>php "C:\test copy\copy.php"
> 'C:\test' is not recognized as an internal or external command,
> operable program or batch file. »
>
> --
> Jean-Marc.


They have to be quoted again in the batch file. Otherwise cmd isn't going to
parse the line correctly.


Reply With Quote
  #7 (permalink)  
Old 01-21-2005
Jean-Marc Molina
 
Posts: n/a
Default Re: Executing an application whose directory path contains blank spaces

Chung Leong a écrit/wrote :
> They have to be quoted again in the batch file. Otherwise cmd isn't
> going to parse the line correctly.


Did you try it ? Does it work ? What's your output ? What's your OS ?

I tried it. It doesn't work. I get below output and my OS is Windows 2000
Pro.

Try to execute the following "copy.php" PHP script :
<?php

system ('"' . dirname ($_SERVER ['PHP_SELF']) . '\mycopy.bat" /B "C:\test
copy\folder1\file1" "C:\test copy\folder2\file2"');

?>

mycopy.bat batch file :
copy "%1" "%2" "%3"

Output :
« >php "C:\test copy\copy.php"
'C:\test' is not recognized as an internal or external command,
operable program or batch file. »

--
Jean-Marc.

Reply With Quote
  #8 (permalink)  
Old 01-21-2005
Paul Barfoot
 
Posts: n/a
Default Re: Executing an application whose directory path contains blank spaces

Jean-Marc

Try executing the command you want directly in a Win2000 command window.
Where do you need to put quotes to get that to work? Then put quotes in
either the batch file or the PHP script to mirror this.

That was how I came to the solution I offered in my earlier post. If I
quoted the source and destination filenames in PHP then the system command
didn't work, but if I didn't put quotes around them the parameters were
treated as 5 variables instead of 3!

Regards
--
Paul Barfoot


"Jean-Marc Molina" <jmmolina@PASDEPOURRIEL-free.fr> wrote in message
news:41f0af46$0$28961$8fcfb975@news.wanadoo.fr...
> Chung Leong a écrit/wrote :
>> They have to be quoted again in the batch file. Otherwise cmd isn't
>> going to parse the line correctly.

>
> Did you try it ? Does it work ? What's your output ? What's your OS ?
>
> I tried it. It doesn't work. I get below output and my OS is Windows 2000
> Pro.
>
> Try to execute the following "copy.php" PHP script :
> <?php
>
> system ('"' . dirname ($_SERVER ['PHP_SELF']) . '\mycopy.bat" /B "C:\test
> copy\folder1\file1" "C:\test copy\folder2\file2"');
>
> ?>
>
> mycopy.bat batch file :
> copy "%1" "%2" "%3"
>
> Output :
> « >php "C:\test copy\copy.php"
> 'C:\test' is not recognized as an internal or external command,
> operable program or batch file. »
>
> --
> Jean-Marc.
>



Reply With Quote
  #9 (permalink)  
Old 01-22-2005
Chung Leong
 
Posts: n/a
Default Re: Executing an application whose directory path contains blank spaces


"Jean-Marc Molina" <jmmolina@PASDEPOURRIEL-free.fr> wrote in message
news:41f0af46$0$28961$8fcfb975@news.wanadoo.fr...
> Chung Leong a écrit/wrote :
> > They have to be quoted again in the batch file. Otherwise cmd isn't
> > going to parse the line correctly.

>
> Did you try it ? Does it work ? What's your output ? What's your OS ?
>
> I tried it. It doesn't work. I get below output and my OS is Windows 2000
> Pro.
>
> Try to execute the following "copy.php" PHP script :
> <?php
>
> system ('"' . dirname ($_SERVER ['PHP_SELF']) . '\mycopy.bat" /B "C:\test
> copy\folder1\file1" "C:\test copy\folder2\file2"');
>
> ?>
>
> mycopy.bat batch file :
> copy "%1" "%2" "%3"
>
> Output :
> « >php "C:\test copy\copy.php"
> 'C:\test' is not recognized as an internal or external command,
> operable program or batch file. »


My apology. I was wrong about the quotes. Didn't realize that cmd is smart
enough to put quotes around a parameter when it expands %1, %2.

The problem, it appears, is caused by a broken implementation of system() in
the C library. To get around, use cmd.exe to parse the command.

Example:

test.bat:
echo %1
echo %2

test.php:

echo `cmd /C ""C:\\test copy\\test.bat" "C:\\Program files" "C:\\Documents
and settings""`

The first and last quotation marks enclose the command, while the others
enclose the parameters. Need to escape slashes here because I'm using
backtick.

Hope that's more useful advise.


Reply With Quote
  #10 (permalink)  
Old 01-22-2005
Jean-Marc Molina
 
Posts: n/a
Default Re: Executing an application whose directory path contains blank spaces

Chung Leong a écrit/wrote :
> The problem, it appears, is caused by a broken implementation of
> system() in the C library. To get around, use cmd.exe to parse the
> command.


Works like a charm !

New copy.php PHP script :
<?php

$cmd = '"' . dirname ($_SERVER ['PHP_SELF']) . '\mycopy.bat" /B "C:\test
copy\folder1\file1" "C:\test copy\folder2\file2"';
system ("cmd /C \"$cmd\"");

?>

New mycopy.bat :
copy %1 %2 %3

Thanks !

--
Jean-Marc.

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 10:18 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0