Finding the owner of a file with ColdFusion

This post is more than 2 years old.

A reader asked me last night if it was possible to find the owner of a file using ColdFusion. You may think getFileInfo would return this but it does not. Luckily it is relatively simple to get to it from Java.

The first result I found via Google sent me to a Stack Overflow answer that worked fine once I converted it to work within ColdFusion:

pathOb = createObject("java", "java.nio.file.Paths");
//Hard coded path here, but obviously this would be dynamic
path = pathOb.get("/Applications/ColdFusion11/cfusion/wwwroot/test.cfm",[]);
files = createObject("java", "java.nio.file.Files");
fav = createObject("java","java.nio.file.attribute.FileOwnerAttributeView");
oav = files.getFileAttributeView(path, fav.getClass(), []);
owner = oav.getOwner();

You could easily wrap this up into a UDF so it is easier to call. Ok, Ray, don't be lazy. Here is a UDF version with an example call:

function getFileOwner(p) {
	var pathOb = createObject("java", "java.nio.file.Paths");
	var path = pathOb.get(p,[]);
	var files = createObject("java", "java.nio.file.Files");
	var fav = createObject("java","java.nio.file.attribute.FileOwnerAttributeView");
	var oav = files.getFileAttributeView(path, fav.getClass(), []);
	return oav.getOwner();


And because I was bored, I rewrote the UDF as one line that would be a pain in the rear to debug.

function getFileOwner(p) {
	return createObject("java", "java.nio.file.Files").getFileAttributeView(createObject("java", "java.nio.file.Paths").get(p,[]), createObject("java","java.nio.file.attribute.FileOwnerAttributeView").getClass(), []).getOwner();
Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA