Showing posts with label Scripting. Show all posts
Showing posts with label Scripting. Show all posts

Saturday, 21 March 2020

Windows Script Host and JScript Revisited

Background

This is a small example Windows Script File (WSF) that can be used in certain work flows.

This work flow involves dealing with files that are created in the In folder.

Due to some issue with a process upstream in this work flow, some of the files that appear in the In folder are missing their file suffix. For reasons outside the scope of this article, this cannot be rectified upstream, and, because it is known that these files all should have a .txt suffix, this script appends the required suffix where there is no suffix.

The script is supplied with an In and an Out folder path.

The Code

<job>
    <script language="JScript">
        // Technology Is Not Dull 2020
        // Get in and out folders as named arguments
        var InFolder = "";
        var OutFolder = "";
        if(WScript.Arguments.Named.Exists("in")){
            InFolder = WScript.Arguments.Named("in");
        }
        if(WScript.Arguments.Named.Exists("Out")){
            OutFolder = WScript.Arguments.Named("Out");
        }
        // If there is an in and an out parameter
        if(OutFolder!="" && InFolder!=""){
            // Create objects to get the files and copy them
            var objFSO=WScript.CreateObject("Scripting.FileSystemObject");
            var objFolder = objFSO.GetFolder(InFolder);
            var colFiles =objFolder.Files;
            // Remove any trailing '\' in supplied Out path
            OutFolder = objFSO.GetFolder(OutFolder);
            // For each file in the In folder
            for(var objEnum = new Enumerator(colFiles); !objEnum.atEnd(); objEnum.moveNext()) {
                sFileName = objEnum.item();
                sOutName = OutFolder +"\\";
                // This assumes that any file with no file suffix
                // is actually a .txt file and adds the suffix for the copied filename
                if(objFSO.GetExtensionName(sFileName) == "")
                {
                    sOutName = sOutName + objFSO.GetFileName(sFileName) + ".txt";
                }
                else
                {
                    sOutName = sOutName + objFSO.GetFileName(sFileName);
                }
                // Copy the file (overwriting any existing file)
                objFSO.CopyFile (sFileName,sOutName,true);
            }
            // Quit with value 0
            WSH.Echo("Done");
            WSH.Quit(0);
        }
        // Else there are one or more missing parameters
        //so quit with error
        else
        {
            WSH.Echo("Missing Named Parameter(s)");
            WSH.Quit(1);
        }
    </script>
</job>

Operation

The script is tested with a folder structure based on "C:\Some Folder\Documents\Javascript\test folders" with two sub folders 1 and 2.

Folder 1
Folder 2

The parameters are named using /<name>:<value>.

C:\Some Folder\Documents>cscript JScriptExample.wsf /In:"C:\Some Folder\Documents\Javascript\test folders\1" /Out:"C:\Some Folder\Documents\Javascript\test folders\2\"
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.

Done

C:\Some Folder\Documents>

As you can see the files have been copied with the suffix added to the file without the suffix.

References

A brief look at JavaScript on various systems including Windows Script Host can be found here.

Monday, 23 September 2019

Javascript

JavaScript is a high-level scripting language that has become one of the core components of the World Wide Web.
Originally used client side in a user’s browser, it has migrated back to the server and is also used for general scripting purposes.

Windows

JavaScript is one of many languages supported by the Windows Scripting Host (WSH).

Hello World

The following is a JavaScript program using the Windows Scripting Host.
The file is called helloworld.js.

WSH.Echo("Hello world");
WSH.Quit();

Create the file with your favourite plain text editor (Notepad or better, but not a word processor, they tend to add lots of tat).

Save the file.

Bring up the command line interface (search for cmd).

Use the change directory command to move to where you have saved the file (if you have spaces in the folder names, enclose the path in double quotes).

To execute the file type:
cscript helloworld.js
This is what you will get (Windows 10 machine - YMMV)

C:\some\folder\tree>cscript helloworld.js
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.

Hello world

C:\some\folder\tree >
An alternative is to use a WSF file (.wsf). This requires the <job> or <package> tags around the script. Because WSF files could contain any supported language (or indeed multiple languages) you need to define the language within a script tag.
<job>
    <script language="JScript">
        WSH.Echo("Hello world");
        WSH.Quit();
    </script>
</job>

C:\some\folder\tree >cscript helloworld.wsf
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.

Hello world

C:\some\folder\tree >

HTML

The following is extremely simple.
The script is executed when the page is loaded but before it is displayed. It is possible to execute the JavaScript after the page has loaded or in response to some event.
<!DOCTYPE html>
<html>
    <head>
        <title>Javascript in HTML</title>
    </head>
    <body>
        <p>Testing Javascript</p>
        <script language="JScript">
            alert("Hello world!");
        </script>
    </body>
</html>
Save the file as helloworld.html.
Because this is displayed in your browser, the display will vary.

Linux

Raspberry Pi

From Raspbian Stretch onwards, most standard installs of Raspbian come with node.js installed as standard.
You can check if it is installed by running:
node -v
On the command line. If it is installed, it will return the version number.
If your installation does not have node.js, install using:
sudo apt-get install node
sudo apt-get install npm
The program is just one line:
console.log("Hello world!");
Using terminal, change the directory to where you have saved the file, you can run it by typing:
node helloworld.js
The result will be similar to:
pi@raspberrypi:~/Documents/JavaScript $ node helloworld.js
Hello world!
pi@ raspberrypi:~/Documents/JavaScript $

Useful functions

Splitting Strings

The following is a Windows specific example.
WSH.Echo("Test code");

WSH.Echo(splitString("-2MKptcmt1Q,W3XlnfNPk6I,dRYRcM-HHvY"));

 function splitString(aString){
    WSH.Echo(aString);
    result = "";
    anArray=[];
    anArray = aString.split(",");
    for(ix = 0;ix<anArray.length;ix++){
        result = result +"*" +(anArray[ix])+"*";
    }
    return result;
}


References

https://en.wikipedia.org/wiki/JavaScript
https://en.wikipedia.org/wiki/ECMAScript
https://www.w3schools.com/js/default.asp
https://www.w3schools.com/jsref/default.asp

https://en.wikipedia.org/wiki/Windows_Script_Host
https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/cscript
https://docs.microsoft.com/en-us/previous-versions//98591fh7(v=vs.85)

https://www.instructables.com/id/Javascripting-Your-RaspberryPi/