将JS代码嵌入批处理文件中 [英] embed JS code in a batch file

查看:108
本文介绍了将JS代码嵌入批处理文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理文件,通过重命名和重新定位来准备word文件。
所以我使用我在本网站找到的javascript代码为这些文件制作pdf。我把它称为如下;

i have a batch file preparing word files by renaming and relocating them. so that i make pdf for this files using a javascript code i ve found in this website. i call it as follows;

for %%g in ("test\*.doc") do (cscript.exe //nologo "SAVEASPDF.js" "%%~fg") 

此JavaScript代码位于另一个以 saveaspdf.js 文件制作PDF。
我可以在批处理文件中嵌入一个JS代码(例如,作为:FUNCTION ),只将所有代码保存在一个文件中?

this JavaScript code is in another file as saveaspdf.js to make PDF. can i embed a JS code inside the batch file (e.g. as a :FUNCTION) to keep all the code in a single file only?

这是我试图嵌入的JS,我在这个网站上找到了它。

here is the JS i m trying to embed, i found it here in this website.

var fso = new ActiveXObject("Scripting.FileSystemObject");
var docPath = WScript.Arguments(0);
docPath = fso.GetAbsolutePathName(docPath);

var pdfPath = docPath.replace(/\.doc[^.]*$/, ".pdf");
var objWord = null;

try
{
    WScript.Echo("Saving '" + docPath + "' as '" + pdfPath + "'...");

    objWord = new ActiveXObject("Word.Application");
    objWord.Visible = false;

    var objDoc = objWord.Documents.Open(docPath);

    var wdFormatPdf = 17;
    objDoc.SaveAs(pdfPath, wdFormatPdf);
    objDoc.Close();

    WScript.Echo("Done.");
}
finally
{
    if (objWord != null)
    {
        objWord.Quit();
    }
}


推荐答案

那里发布了许多方法,用于在批处理脚本中嵌入和执行JScr​​ipt。以下是一些:

There are many methods posted for embedding and executing JScript within a batch script. Here are a few:


  1. https://stackoverflow.com/a/15169687/1012053

    这是我最喜欢的,我将在下面使用的那个

  1. https://stackoverflow.com/a/15169687/1012053
    This is my favorite, and the one I will use below

https://stackoverflow.com/a/4999378/1012053

我不喜欢不喜欢这种方法,因为它定义了一个额外的(不需要的)环境变量。

https://stackoverflow.com/a/4999378/1012053
I don't like this method because it defines an extra (unwanted) environment variable.

https://stackoverflow.com/a/15176096/1012053 (编辑前)

另一个很好的选择。

https://stackoverflow.com/a/15176096/1012053 (before the EDIT)
Another excellent choice.

https://stackoverflow.com/a/9074483/1012053 (最终更新2014-04-底部的27)

这种WSF技术不太方便,但功能强大,因为你可以在一个批处理脚本中嵌入和执行任意数量的独立JScript和/或VBS作业。

https://stackoverflow.com/a/9074483/1012053 (The final UPDATE 2014-04-27 at the bottom)
This WSF technique is not quite as convenient, but it is powerful in that you can embed and execute any number of independent JScript and/or VBS jobs within a single batch script.

S. o以下是如何使用选项1.将两个脚本合并为一个文件:

So here is how you could use option 1. to combine the two scripts into a single file:

@if (@X)==(@Y) @end /* Harmless hybrid line that begins a JScript comment

:: ******* Begin batch code *********
@echo off
for %%g in ("test\*.doc") do cscript //E:JScript //nologo "%~f0" "%%~fg"
exit /b

********* Begin JScript code **********/
var fso = new ActiveXObject("Scripting.FileSystemObject");
var docPath = WScript.Arguments(0);
docPath = fso.GetAbsolutePathName(docPath);

var pdfPath = docPath.replace(/\.doc[^.]*$/, ".pdf");
var objWord = null;

try
{
    WScript.Echo("Saving '" + docPath + "' as '" + pdfPath + "'...");

    objWord = new ActiveXObject("Word.Application");
    objWord.Visible = false;

    var objDoc = objWord.Documents.Open(docPath);

    var wdFormatPdf = 17;
    objDoc.SaveAs(pdfPath, wdFormatPdf);
    objDoc.Close();

    WScript.Echo("Done.");
}
finally
{
    if (objWord != null)
    {
        objWord.Quit();
    }
}

这篇关于将JS代码嵌入批处理文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆