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

3 Answers3

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();
    }
}
Community
  • 1
  • 1
dbenham
  • 119,153
  • 25
  • 226
  • 353
0

Try this:

ECHO 'YOUR_JS_CODE' | node

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

lleaff
  • 3,725
  • 13
  • 22
  • 1
    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. – SomethingDark Dec 29 '15 at 00:22
  • For multiple lines they could use this trick: http://stackoverflow.com/questions/3294599/do-batch-files-support-multiline-variables , 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? – lleaff Dec 29 '15 at 00:43
  • 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. – lleaff Dec 29 '15 at 01:17
  • 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. – SomethingDark Dec 29 '15 at 01:20
  • thanks, it is not one line, i added the saveaspdf.js – hakanSD Dec 29 '15 at 09:04
-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.

Karl-Johan Sjögren
  • 14,076
  • 7
  • 55
  • 62
  • 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](http://stackoverflow.com/a/34512715/1012053) – dbenham Dec 29 '15 at 14:08