代码之家  ›  专栏  ›  技术社区  ›  Desolator

wscript.shell使用php运行路径中有空格的文件

  •  0
  • Desolator  · 技术社区  · 14 年前

    我试图使用wscript.shell通过带有php的com对象将一些命令传递给curl库(dos版本)。以下是我用来执行此任务的内容:

    function windExec($cmd,$mode=''){
        // Setup the command to run from "run"
        $cmdline = "cmd /C $cmd";
    
        // set-up the output and mode
        if ($mode=='FG'){
            $outputfile =  uniqid(time()) . ".txt";
            $cmdline .= " > $outputfile";
            $m = true;
        }
        else $m = false;
    
        // Make a new instance of the COM object
        $WshShell = new COM("WScript.Shell");
    
        // Make the command window but dont show it.
        $oExec = $WshShell->Run($cmdline, 0, $m);
    
        if ($outputfile){
            // Read the tmp file.
            $retStr = file_get_contents($outputfile);
            // Delete the temp_file.
             unlink($outputfile);
        }
        else $retStr = "";
    
        return $retStr;
    }
    

    现在,当我运行这个函数时:

    windExec("\"C:/Documents and Settings/ermac/Desktop/my project/curl\" http://www.google.com/", 'FG');
    

    curl不运行,因为路径有问题。但是当我把这些空间从小路上移开的时候,效果很好。

    windExec("\"C:/curl\" http://www.google.com/", 'FG');
    

    所以我的问题是如何在wscript.shell命令中转义这些空格? 我能解决这个问题吗?

    提前谢谢:)

    1 回复  |  直到 14 年前
        1
  •  0
  •   Desolator    14 年前

    我找到了一个解决方案: 那里:

    windExec("cd C:/Documents and Settings/ermac/Desktop/my project/libs & curl.exe -L http://www.google.com/", 'FG');