Search code examples
javascriptfilefirefoximacros

Imacros javascript If file (A) not found, delete file (B)


If file A exist, break the code.

if the file A doesn't exist, delete B file

My code keeps deleting the B file, it does not matter if the file A is present..

iimSet("file",f);
var  f = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile); file.initWithPath("C:\\A.txt"); 

if (f == 1) { 
// file exists 
iimPlay("CODE:WAIT SECONDS=3")
} else { 
// file doesn't exist 
if (f != 0)

var g = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile); file.initWithPath("C:\\B.txt"); 
file.remove(true);
break;    
}

Solution

  • I suggest using something like this:

    if (iimPlay("CODE:SET !DATASOURCE C:\\A.txt") == 1) {
        // file exists 
        iimPlay("CODE:WAIT SECONDS=3");
    } else {
        // file doesn't exist 
        iimPlay("CODE:FILEDELETE NAME=C:\\B.txt");
    }