【发布时间】:2016-02-25 22:34:59
【问题描述】:
我正在使用 JScript 创建这个 HTA 应用程序,它会编写一个 BAT 文件,然后在 cmd 中打开它。
如果我从 Windows 手动打开创建的 BAT 文件,它会正常工作。
当我通过我的 HTA 打开它时,文件打开但只输出 Echo 和暂停。
请记住,我是编程新手。
这是 JScript。
// Write the Bat file
// When run from HTA all I get is the "Echo Done" and "Pause"
// When I run from Windows without HTA, all of it works.
function writeBat() {
var fso = new ActiveXObject("Scripting.FileSystemObject");
var s = fso.CreateTextFile("C:/test/test.bat");
s.WriteLine('@echo off');
s.WriteLine('set output=C:/test/new/');
s.WriteLine('FOR %%a in (*.mkv) DO ("C:/Program Files/MKVToolNix/mkvmerge.exe" -o "%output%%%~na.mkv" "%%a")');
s.WriteLine('echo Done.');
s.WriteLine('pause');
s.Close(); }
// Run the Bat file.
function runBat() {
var MyObject = new ActiveXObject("wscript.shell");
MyObject.Run("C:/test/test.bat"); }
这是批处理文件。
@echo off
set output=C:/test/new/
FOR %%a in (*.mkv) DO ( "C:/Program Files/MKVToolNix/mkvmerge.exe" -o "%output%%%~na.mkv" "%%a")
echo Done.
pause
还有 HTML
<form>
<input type="button" value="Write Bat" onClick="writeBat()">
<input type="button" value="Run Bat" onClick="runBat()">
</form>
【问题讨论】:
-
在批处理文件中添加一个
echo %cd%以检查运行目录,如果需要添加额外的pushd c:\directory行。 -
JavaScript 和 JScript 虽然语法相似,但都是not equivalent。你这里写的是JScript。
-
正如我在帖子中所说,我对此很陌生。感谢您清理 Jscript 的事情。至于答案,
%cd%显示到桌面的路径,所以我在顶部添加了s.WriteLine('pushd "%~dp0"');,它可以工作!!非常感谢!
标签: html batch-file jscript hta