Check if a file exists using a script

I’m trying to check if a file exists in a script so I can ensure there aren’t an image errors in my printer template.

I tried the following but the output doesn’t appear to be as expected. Whats wrong with this?

function imagecheck(tid) {

	var file = host.type("System.IO.Directory");
	var filename = "C:\\Barcode\\images\\" + tid + ".png";
	var check = file.Exists(filename);
	
	if (!file.Exists(filename)) {
		dlg.ShowMessage("Image does not exist" + filename);
		dlg.ShowMessage(check);
		return '';
	} else {
		return ("<div style=\"text-align:center\";><img src="+ filename + ".png></div>");
	}

}

Nevermind the issue is:

(1) the variable Check isn’t a string so the dialog box doesn’t like it. That can be fixed with toString().
(2) the host type is wrong. Directory is used for checking a folder exists, but file is needed for a file.

Working now.

function imagecheck(tid) {

	var file = host.type("System.IO.File");
	var filename = "C:\\Barcode\\images\\" + tid + ".png";
	// var check = file.Exists(filename).toString();
	
	if (!file.Exists(filename)) {
		dlg.ShowMessage("Image does not exist" + filename);
		// dlg.ShowMessage(check);
		return '';
	} else {
		return ("<div style=\"text-align:center\";><img src="+ filename + ".png></div>");
	}

}
1 Like