【问题标题】:JSFL: FLfile.runCommandLine & properly escaping spaces for Windows commandline argsJSFL:FLfile.runCommandLine & 为 Windows 命令行参数正确转义空格
【发布时间】:2012-02-25 08:57:11
【问题描述】:

我正在开发一个 JSFL 脚本,该脚本将导出 WAV 文件并使用 lame.exe 通过 FLfile.runCommandLine 将它们编码为 MP3。我不知道如何正确转义命令行中的空格以使其正常工作。

var command_line = '"C:\pathWithSpaces in pathname\lame.exe" -option1 -option2 "C:\different pathWithSpaces\targetfile.wav" "C:\different pathWithSpaces\targetfile.mp3"' ;
FLfile.runCommandLine (command_line);

命令窗口中的结果:

'C:\pathWithSpaces' 未重新识别为内部或外部 命令、可运行的程序或批处理文件。

我试过用'%20'和carrat-space'^'替换空格,都失败了。 var command_line 在手动剪切并粘贴到命令窗口时被验证可以工作,只有在 JSFL 脚本中运行表单时,空格似乎才是问题。

(简单地从环境中的任何路径中删除空格不是一种选择。命令行变量是动态生成的,必须能够处理空格以对其他人有用。)

【问题讨论】:

    标签: windows command-line-arguments jsfl


    【解决方案1】:

    我想我找到了你的答案。您需要额外的周边报价。

    var filePath = '"c:/somepath"'
    var argument = '"argument"'
    FLfile.runCommandLine('"'+ filePath + ' ' + argument +'"');
    

    所以你最终传递了一些看起来像

    的东西
    ""c:/somepath" "argument""
    

    注意周围的附加引号

    【讨论】:

      【解决方案2】:

      您根本不需要运行 .bat 文件。您的问题是在调用runCommandLine 之前,您没有将可执行URI 的路径转换为平台路径。您的代码应如下所示:

      var exe_path = FLfile.uriToPlatformPath("C:\pathWithSpaces in pathname\lame.exe");
      
      var command_line ='"' + exe_path + '" -option1 -option2 "C:\different pathWithSpaces\targetfile.wav" "C:\different pathWithSpaces\targetfile.mp3"';
      
      FLfile.runCommandLine (command_line);
      

      【讨论】:

        【解决方案3】:

        在 Dave 的带领下,我最终得到了以下代码:

        //get users temp folder& convert to URI
        var win_tempLamePath =FLfile.getSystemTempFolder()+'lame.bat';
        var win_tempLameURI =FLfile.platformPathToURI(win_tempLamePath);
        //generate proper syntax for windows CMD
        var win_fileURI = (FLfile.uriToPlatformPath(<URI for target WAV file>);
        var win_command =('"'+win_uri+'lame.exe" -V0 -h "' + win_fileURI + '.' + wav +'" "' + win_fileURI + '.mp3" 2> "'+ win_fileURI+'.txt'+'"');
        //write the command to lame.bat(aka win_tempLameURI)  & execute
        FLfile.write(win_tempLameURI, win_command);
        FLfile.runCommandLine(win_tempLamePath);
        

        win_command 末尾的注释块

         2> "'+ win_fileURI+'.txt'+'"
        

        是将 LAME.EXE 输出到文本文件。通常 ">" 在 windows cmd 中执行此操作,但 LAME.EXE 使用一种奇怪的输出方法,需要 "2>" 才能达到相同的效果,正如我在this thread 中学到的那样

        【讨论】:

          【解决方案4】:

          你知道,我可能错了!我尝试了很多选择,但没有运气。我认为这可能与多个论点有关……不进一步调查就不确定。

          一个简单的解决方法是将命令保存到批处理文件然后运行:

          var command = '"C:/pathWithSpaces in pathname/lame.exe" -option1 -option2 "C:/different pathWithSpaces/targetfile.wav" "C:/different pathWithSpaces/targetfile.mp3"';
          FLfile.write('file:///C|/temp/lame.bat', command);
          FLfile.runCommandLine('"c:/temp/lame.bat"');
          

          希望有帮助:)

          【讨论】:

          • 我们希望避免这种方法,但它可能是唯一的选择。谢谢戴夫!
          • 我不会担心的。 xJSFL 为一堆幕后任务向硬盘写入了相当多的临时文件,这没问题!
          • 顺便说一句——如果你想在不锁定Flash的情况下运行批处理文件,你也可以使用START命令:robvanderwoude.com/ntstart.php
          【解决方案5】:

          这可能不是问题。您需要转义反斜杠: C:\\pathWithSpaces in pathname\\lame.exe"

          另一种方法是使用正斜杠,windows 也可以理解。

          【讨论】:

          • 嘿,你不是 xJSFL 的人吗? :) 我会试试这个。
          • 就是我!不过,我通常在这里作为计划老我:) 你在使用框架吗?
          • 尚未使用 xJSFL。我厌倦了反转斜线,但结果相同。你有这个工作的例子吗?
          猜你喜欢
          • 2016-06-15
          • 1970-01-01
          • 1970-01-01
          • 2013-09-22
          • 2018-11-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-03-31
          相关资源
          最近更新 更多