Question

Here I am trying to check that if file cmd.bat exist in temp folder then doesn't create new one, otherwise create a cmd.bat in temp folder. But having problem with temp folder path "%temp%\cmd.bat", it is displaying error :

Timestamp: 10/31/2012 3:21:40 AM
Error: NS_ERROR_FILE_UNRECOGNIZED_PATH: Component returned failure code: 0x80520001                  (NS_ERROR_FILE_UNRECOGNIZED_PATH) [nsILocalFile.initWithPath]
Source File: chrome://myext/content/overlay.js
Line: 9           

My code :

        var chk_file = Components.classes["@mozilla.org/file/local;1"]
             .createInstance(Components.interfaces.nsILocalFile);

        chk_file.initWithPath("%temp%\cmd.bat");

        if ( chk_file.exists() == false ) {
            Components.utils.import("resource://gre/modules/FileUtils.jsm");

            var file = FileUtils.getFile("TmpD", ["cmd.bat"]);
            file.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE);
        }
        else
        {
            alert("file already exists");
        }
Was it helpful?

Solution

initWithPath takes full system path only.

Use this to go to temp and then try

var environment = Components.classes["@mozilla.org/process/environment;1"]
                            .getService(Components.interfaces.nsIEnvironment);


var path = environment.get("TEMP");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top