0

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

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?

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();
    }
}

3 Answers 3

2

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
    This is my favorite, and the one I will use below

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

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

  4. 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.

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();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

ECHO 'YOUR_JS_CODE' | node

http://www.robvanderwoude.com/redirection.php

5 Comments

If his javascript code is only one line long and he has node.js installed, sure. But there are way better ways to do that.
For multiple lines they could use this trick: stackoverflow.com/questions/3294599/… , and given that they are talking about making a PDF using a local .exe from JS code, it's safe to assume they have node.js installed. Also if you know of a better way why not create an answer explaining it?
Ok, FYI I don't think that kind of comment on a working solution is helpful at all. OP wanted a solution and I gave him one quick. If your fear is that a weak answer gets accepted and recorded for posterity then you can always post an answer later, I will gladly remove mine if yours is genuinely better.
Well my argument was that your solution might not be reasonable, depending on how long OP's code is. Until saveaspdf.js is posted, it's impossible to say.
thanks, it is not one line, i added the saveaspdf.js
-1

If you look at the documentation for cscript.exe it seems that it wants to read the script from a file so I believe you are out of luck here.

Running it via nodejs as lleaff suggested might have worked if your script was compatible with nodejs but since you are using ActiveXObject to automate Word it wouldn't execute.

1 Comment

Well, the batch script is a file, so CSCRIPT can read the JScript code embedded within the batch script. It isn't very difficult. See my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.