JSFL:FLfile.runCommandLine和在Windows命令行参数中正确转义空格

3

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

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' 和尖角号空格'^ '替换空格,但都失败了。当手动剪切并粘贴到命令窗口时,变量 command_line 被验证可用,只有在从 JSFL 脚本中运行时,空格才成为问题。

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

5个回答

0
跟随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命令提示符中,通常使用“>”来实现此功能,但LAME.EXE使用一种奇怪的输出方法,需要使用“2>”才能达到相同的效果,正如我在this thread中所学到的。


0

我想我找到了你的答案。你需要添加一个额外的引号。

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

所以你最终传递的东西看起来像这样

""c:/somepath" "argument""

注意额外的引号


0
你根本不需要运行一个.bat文件。你的问题在于,在调用runCommandLine之前,你没有将可执行文件的路径转换为平台路径。你的代码应该像这样:
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);

0

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

另一种方法是使用正斜杠,Windows 也能理解。


1
嘿,你不是那个xJSFL的家伙吗? :) 我会试一下。 - Mambo4
那是我!但通常我在这里使用我的普通账号 :)你正在使用框架吗? - Dave Stewart
不过我还没有使用 xJSFL。我尝试了反转斜杠,但结果相同。您有这方面的工作示例吗? - Mambo4

0

你知道吗,我可能对这个问题并不正确!我尝试了很多选项但没有运气。我认为这可能与多个参数有关......没有进一步调查,我不确定。

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

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"');

希望这有所帮助 :)

我们本来希望避免采用那种方法,但这可能是唯一的选择。谢谢,戴夫! - Mambo4
不用担心。xJSFL会为一些幕后任务写入相当多的临时文件到硬盘,这并不是问题! - Dave Stewart
顺便提一下 - 如果您想在不锁定 Flash 的情况下运行批处理文件,也可以使用 START 命令:http://www.robvanderwoude.com/ntstart.php - Dave Stewart

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接