Resolve a local UNC path to a physical drive path

Since we have Path.ToUNC(); is there a quick way to convert a UNC path that points to my own local machine into its actual physical drive path? Basically resolving something like \\COMPUTERNAME\myshare\file.txt back to C:\path..... I could see other uses beside scripting as well, but I couldn't find anything related in the docs. Any tips? Maybe a new Path.FromUNC()?

We added the ToUNC method (and some similar things outside scripting) to help when sharing network paths with other people on the same network who might not have the same drive letters.

Are there times when the reverse is needed? (Presumably so, else you wouldn't be asking. :slight_smile: But I can't think of them.)

This should do what you're expecting.
A bit tricky because, if I understood correctly, you also want to identify local shares, so you need to search them with WMI (or at least it is a way to do it).
I tend not to like to revert to calling ActiveXObjects ... but couldn't find another way here.

function convertUncToDrivePath(uncPath) {
    try {
        var wshNetwork = new ActiveXObject("WScript.Network");
        var computerName = wshNetwork.ComputerName.toLowerCase();
        
        // Normalize UNC (lowercase + backslashs)
        var cleanUNC = uncPath.toLowerCase().replace(/\//g, "\\");
        
        if (cleanUNC.indexOf("\\\\") === 0) {
            var parts = cleanUNC.substring(2).split("\\");
            var targetComputer = parts[0];
            var shareName = parts[1] || "";
            
			// Case 1: local computer
            if (targetComputer === computerName || targetComputer === "localhost" || targetComputer === "127.0.0.1") {
                
                // 1.a : \\ComputerName\C$\...
                if (shareName.length === 2 && shareName.charAt(1) === "$") {
                    var driveLetter = shareName.charAt(0).toUpperCase() + ":";
					var originalParts = uncPath.substring(2).split("\\");
                    var subParts = originalParts.slice(2);
                    return driveLetter + "\\" + subParts.join("\\");
                }
                
                // 1.b: \\Computer_Name\SharedFolderName)
                // Use WMI (CIM)
                var localPhysicalPath = getLocalSharePath(shareName);
                if (localPhysicalPath) {
                    // Add subfolders if any
					var originalParts = uncPath.substring(2).split("\\");
                    if (originalParts.length > 2) {
                        var subPath = originalParts.slice(2).join("\\");
                        if (localPhysicalPath.slice(-1) !== "\\") localPhysicalPath += "\\";
                        return localPhysicalPath + subPath;
                    }
                    return localPhysicalPath;                }
            }
        }
        
        // Case 2: Remote network drive
        var oDrives = wshNetwork.EnumNetworkDrives();
        for (var i = 0; i < oDrives.length; i += 2) {
            var driveLetter = oDrives.Item(i);     // Ex: "Z:"
            var driveUNC    = oDrives.Item(i + 1); // Ex: "\\serveur\partage"
            
            if (!driveUNC) continue;
            
            var cleanDriveUNC = driveUNC.toLowerCase().replace(/\//g, "\\");
            
            if (cleanUNC === cleanDriveUNC || cleanUNC.indexOf(cleanDriveUNC + "\\") === 0) {
                var subPath = uncPath.substring(driveUNC.length);
                return driveLetter + subPath;
            }
        }
        
    } catch (e) {
        DOpus.Output("Error : " + e.message);
    }
    
    // Default return: input UNC
    return uncPath;
}


function getLocalSharePath(shareName) {
    try {
        var locator = new ActiveXObject("WbemScripting.SWbemLocator");
        var service = locator.ConnectServer(".", "root\\cimv2");

        var query = "SELECT Path FROM Win32_Share WHERE Name = '" + shareName.replace(/'/g, "''") + "'";
        var shares = service.ExecQuery(query);
        
        var enumShares = new Enumerator(shares);
        if (!enumShares.atEnd()) {
            var share = enumShares.item();
            if (share && share.Path) {
                return share.Path; // Returns physical path (ex: "K:\MEDIA\MUSIQUE_PROD\2_SOFTWARE")
            }
        }
    } catch (e) {
        DOpus.Output("WMI Error");
    }
    return null;
}