I know JavaScript is a client side browser-buddy language. But, just for kicks I wanted to try out JavaScript on command line. Here is the story…

I was working on a codebase in Windows machine where we have very very long file names. As you may know, Windows XP don’t support file names longer than 260 characters. It was very annoying when a maven build fails due to the file name length limitation. So, I wanted to write a script which can recursively loop through the project codebase and list out the files which exceeds certain length limit, so I can work on shortening their names. I could write that script in different ways like batch script, shell script, groovy script etc. But, I just wanted to write it in plain simple JavaScript and see how it plays.

If you want to try out the script. Copy the following code block in a JS file (For ex. FileNameLengthChecker.js).

/*
 Simple utility script to look for file names exceeding certain length restriction.
 (esp. for Windows XP - http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx#maxpath).

 If you want to run check for a folder other than the current folder, change the value of
 rootDir variable.
 */

var filenameLengthThreshold = 260;
var headerPrinted = false;
var totalFilesFound = 0;

function runFileNameLengthCheck() {
    var rootDir = new java.io.File(".");
    echo("Checking file name length check in '" + rootDir.getCanonicalPath() + "' for threshold limit = " + filenameLengthThreshold + " ...");

    checkFilePathRecursively(rootDir);

    if (!headerPrinted) {
        echo("\n----------------------------------------------------");
        echo("No files found with the given threshold limit !!!");
        echo("---------------------------------------------------");
    }
    else {
        echo("Total files found = " + totalFilesFound);
    }

}

function printHeader() {
    if (!headerPrinted) {
        echo("\n------------------------------------------------");
        echo("Following files exceeded the file name length check...");
        echo("------------------------------------------------");
        headerPrinted = true;
    }
}

function checkFilePathRecursively(currentFolder) {
    var files = currentFolder.listFiles();
    for (var i = 0; i < files.length; i++) {
        if (files[i].isDirectory()) {
            checkFilePathRecursively(files[i])
        }
        else {
            var path = files[i].getCanonicalPath();
            if (path.length() > filenameLengthThreshold) {
                printHeader();
                echo("Filename = '" + path + "', length = " + new java.lang.Integer(path.length()));
                totalFilesFound++;
            }
        }
    }
}

runFileNameLengthCheck();

The simplest way to run the script file from a command line is through jrunscript tool bundled within JDK.

	C:\ProjectCodeBase\jrunscript FileNameLengthChecker.js

If you use IntelliJ IDE and Script Monkey plugin installed, you can directly run the script from IDE.

This is just a trivial example, but the possibilities of what we can do using the same technique is endless. Thoughts? Comments?

2 thoughts on “Let’s do some command line JavaScripting…

  1. You can run JavaScript at the command line without Java or JDK or anything other than a plain Windows XP installation, because it includes JScript as part of Windows Script Host. You would need to use the FileSystemObject in place of java.io.File for your filename checking. You can even run it with no extension thanks to the PATHEXT environment variable.

    C:\>copy con JSTest.js
    WScript.Echo(“this is JavaScript”)
    ^Z
    1 file(s) copied.

    C:\>jstest
    this is JavaScript

  2. @Siddique
    Thank you for your plugin and for all these tips!

    @Todd:
    1st: You need windows for this.
    2nd: JScript isn’t JavaScript – it is JScript ;-)
    3rd: from jrunscript you can access all Java and it’s libraries ultimate power!

Leave a reply

required

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>