2010-12-07 144 views
0

我试图从命令行使用PHP运行.bat文件。我正在使用Windows Vista家庭高级版。PHP +命令行执行

当我在像ipconfig.exe这样的文件上使用脚本时,我得到输出。但是,当我运行.bat文件时,它会输出文件中的内容,但不会执行它。

下面是什么作品,并给我输出:

$runCommand = "C:\\WINDOWS\\system32\\ipconfig.exe"; 
$WshShell = new COM("WScript.Shell"); 
$output = $WshShell->Exec($runCommand)->StdOut->ReadAll; 
echo "<p>$output</p>"; 

但这并不:

$runCommand = "C:\\Temp\\foo.bat"; 
$WshShell = new COM("WScript.Shell"); 
$output = $WshShell->Exec($runCommand)->StdOut->ReadAll; 
echo "<p>$output</p>"; 

下面是在我的foo.bat文件:

C:/windows/system32/schtasks.exe /create /tn "TestTask" /tr "C:/Temp/configure.php" /sc minute /st 08:00:00 

如果我复制这个并粘贴到我的Windows命令行中,此命令执行成功。

不知道发生了什么事。请协助。

+1

12的重复问题,1接受答案。请阅读FAQ:http://stackoverflow.com/faq – 2010-12-07 18:25:55

回答

1

这是因为bat文件是用于提示的排队列表的命令。尝试以下方法:

cmd /c myfile.bat

(它可能是/ K太,忘记其执行和关闭)

此外,How do you run a .bat file from PHP?

EDIT

<?php 
    // http://www.php.net/manual/en/function.exec.php#85930 

    $_ = null; 

    // If you care about the return value, use this: 
    passthru("C:\\WINDOWS\\system32\\cmd.exe /c custom.bat",$_); 
    header('Content-Type: text/plain'); 
    echo $_; 
    // if you don't care, just use this: 
    $_ = exec("C:\\WINDOWS\\system32\\cmd.exe /c custom.bat"); 
?> 
+0

我将C:\\ Temp \\ foo.bat替换为C:\\ WINDOWS \\ system32 \\ cmd.exe/k C:\\ Temp \\ foo.bat但它仍然不会执行。 – 2010-12-07 18:35:13