How to get file owner name?
Is there a perl script to add own开发者_运维问答er's/authors name of the file?
my $owner = getpwuid((stat($file))[4]);
see stat and getpwuid for more detail.
Update: for Windows,
from this post: http://www.perlmonks.org/?node_id=865219
use Win32::OLE;
my $objShell = Win32::OLE->CreateObject("Shell.Application");
my $objFolder=$objShell->Namespace("c:\\a") or die "$!" ;
my $a = $objFolder->ParseName("a.txt") or die "$!" ;
print $objFolder->GetDetailsOf($a, 8) or die "$!" ;
or,
use Win32::Perms;
my $username = Win32::Perms->new($filename)->Owner;
#!/usr/bin/perl -w
my @sb = stat "/etc/passwd";
my $userid = $sb[4];
my @pwent = getpwuid $userid;
my $username = $pwent[0];
print "/etc/passwd is owned by $username\n";
$ /tmp/foo.pl
/etc/passwd is owned by root
The perldoc perlfunc
guide has lots of information on these families of functions.
精彩评论