Question

I created a variable to hold a relative path because I cannot have a direct path since this will be installed on different PCs.

var mainUrlCONST = "../../annotations/annotate.xml";

When the initial page of the program loads, it checks to see if the file exists. If it does not, it creates it.

Now here-in lies the problem, it reads from exactly where I want it to, but if it does not see the file there, it creates it somewhere else on the PC instead of the location I specified.

This checks for the file:

function initializeAnnotationFile()
{
    try
    {
        var connection = new ActiveXObject("Microsoft.XMLHTTP");
        connection.open("GET", mainUrlCONST, false);
        connection.send();

        if ( connection.readyState == 4 )
        {
        response = connection.responseText;
        }

        xml = response; 
        mainExists = true;
    }
        catch(e)
    {
        mainExists = false; 
    }
}

This creates the file if it does not exist:

function createAnnotationFile()
{
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var s = fso.CreateTextFile(mainUrlCONST, true);

    s.WriteLine( "<list>" );
    s.WriteLine( "  <section title='Annotations'>" );
    s.WriteLine( "  </section>" );
    s.WriteLine( "</list>" );
}

Here is the direct path that works, if it will help any.

var mainUrlCONST = "G:/folder/annotations/annotate.xml";
Was it helpful?

Solution

There has been problems with FSO and relative paths. All documentation says, that paths can be either absolute or relative, but personally I never have got relative paths to work.

I'm using an installation folder -based addressing system in my local apps. A simplified version is something like this:

function getInstallBase() {
    var defInstal = 'Application_installation_folder_name',
        selfPath = window.location.pathname.replace(/\\/g,'/');
    if (selfPath.charAt(0) === '/') { // *
        selfPath = selfPath.substring(1, selfPath.length);
    }
    selfPath = selfPath.split(defInstal);
    return selfPath[0] + defInstal + '/';   
}
var defRoot = getInstallBase();

* = IE returns /G:/... when HTA returns G:/...

defRoot now contains an absolute path to the installation folder, no matter where it is saved.

Put this code to a JS-file in the installation folder of your application. Where ever you need a path, provide it based on defRoot i.e. counted from the installation folder. In your case (assuming folder is the installation folder) you can use it like this:

var s = fso.CreateTextFile(defRoot + 'annotations/annotate.xml', true);

I've used this technique for portable apps, and it works like a charm. You can make executable copies to memory sticks, CDs, DVDs, where ever you want, without need to touch the code at all.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top