how to get owner name of the file/directory in windows

karim: Hello All,

I am posting the below code so that people who are searching for "how to find owner of a file in windows" will be benefited.

I searched a lot to find such code but in vain. Here it is...

Pls post any comments about it.

        proc getOwner { filePath}\
        {
                set owner ""
                global tcl_platform
                
                if {$tcl_platform(platform) eq "windows"}\
                {
                        set filePath [file nativename $filePath]
                        if {[file exists $filePath]}\
                        {
                                if {[file type $filePath] eq "file"}\
                                {
                                        if { ![catch {set str [exec cmd /K dir $filePath /Q] }]}\
                                        {
                                                # puts $str
                                                set match [file tail $filePath]
                                                set exp ""; append exp \{; append exp {[[:space:]]([^[:space:]]*)[[:space:]]*\m} $match {\M}; append exp \}

                                                # puts $exp
                                                regexp [join $exp] $str v owner
                                        }
                                }\
                                elseif {[file type $filePath] eq "directory"}\
                                {
                                        if { ![catch {set str [exec cmd /K dir $filePath /Q] }]}\
                                        {
                                                # puts $str
                                                set match [file tail $filePath]
                                                set exp ""; append exp \{<DIR>; append exp {[[:space:]]*([^[:space:]]*)[[:space:]]*}; append exp \}

                                                # puts $exp
                                                regexp [join $exp] $str v owner
                                        }
                                }
                        }
                }\
                elseif {$tcl_platform(platform) eq "unix"}\
                {
                        set owner [file attributes $filePath -owner]
                }
                
                set owner
        }

Regards,

Karim


AMG: I took the liberty of cleaning up a few style issues, mostly removing excess quoting. Sorry, I haven't had a chance to check out the behavior of the code. Anyway, I think I should warn you about braces. Your code would have mysteriously failed had you not been lucky enough to have a matched number of opening and closing braces inside quotes. Putting braces inside double quotes doesn't quite work as you expect. To be safe, you must precede them with backslashes.

Here's some nonworking code:

proc print_braces_1 {} {
    puts "}{"
}

This immediately results in "extra characters after close-brace" when you type it in.

And here's some (accidentally) working code:

proc print_braces_2 {} {
    puts "{}"
}

Wild, huh? Here's the safe and correct way:

proc print_braces_1_fixed {} {
    puts "\}\{"
}
proc print_braces_2_fixed {} {
    puts "\{\}"
}

To understand what's happening, let's look at [print_braces_1] again. What are the arguments to [proc]? First is print_braces_1, second is empty string, and third is the script body. Well, you may notice that the third argument is quoted via braces. How, exactly, does brace quoting work? Here's the official documentation: [L1 ].

If the first character of a word is an open brace (“{”) and rule [5] does not apply, then the word is terminated by the matching close brace (“}”). Braces nest within the word: for each additional open brace there must be an additional close brace (however, if an open brace or close brace within the word is quoted with a backslash then it is not counted in locating the matching close brace). No substitutions are performed on the characters between the braces except for backslash-newline substitutions described below, nor do semi-colons, newlines, close brackets, or white space receive any special interpretation. The word will consist of exactly the characters between the outer braces, not including the braces themselves.

The problem is really obvious when you strip out the extra whitespace in the original code:

proc print_braces_1 {} {puts "}{"}
                       ^      ^xxx

I put ^ caret marks at the matching braces. Tcl sees more text following the close brace (marked above with x's), so it aborts. Tcl's only mechanism for disabling brace counting is to precede the brace with a backslash, so the double quote characters don't help. Now, here's the fixed version:

proc print_braces_1_fixed {} {puts "\}\{"}
                             ^           ^

The backslashes selectively turn off brace counting, so there's no confusion as to where the argument word ends.