Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Monks, Name of the file needs to be pulled off the end of $filename. Then run an regex on $owner to remove the file name.
# F full output my $F = `dir /q "$filename"`; my @lines = split /\n/, $F; # split output line by line # Owner line is on line 6 (Array element 5.) my $OL = $lines[5]; unless ( $lines[5] ) { return "UNKNOWN\\Windows"; } # The owner is the fourth word, my @words = split / +/, $OL; my $owner = $words[3]; # Return to the directory we were called from. $self->change_directory($currentdir); # Return the owner. return $owner;
My regex foo is weak any help would be most appreciated

2010-04-23 Retitled by GrandFather, as per Monastery guidelines

Replies are listed 'Best First'.
Re: How to get filesystem ownerships
by SuicideJunkie (Vicar) on Apr 22, 2010 at 14:17 UTC
      You might want to check how windows achieves posix compliance There is a reason I didn't use stat..thanks though!

        You have to work with Windows, and you know that stat() doesn't give you the information that you need. So why don't you tell us that fact in your original posting? How should anyone be able to know that you are not the ten millionth newbie trying to read a directory by invoking dir, ls or the like, if you don't tell us?

        Apart from that, I doubt that invoking dir /q is reliable. At least on my german WinXP, it cuts off the account name and appends the file name, without any white space to split on:

        C:\WINDOWS>dir /q
         Datenträger in Laufwerk C: ist System
         Volumeseriennummer: CC40-C624
        
         Verzeichnis von C:\WINDOWS
        
        21.04.2010  17:44    <DIR>          VORDEFINIERT\Administra.
        21.04.2010  17:44    <DIR>          VORDEFINIERT\Administra..
        23.04.2010  09:20                 0 VORDEFINIERT\Administra0.log
        27.03.2009  09:54    <DIR>          VORDEFINIERT\Administraaddins
        03.05.2005  19:43            69.632 LUE-ABTEILUNG-1\user   Alcmtr.exe
        04.05.2006  17:26         2.808.832 LUE-ABTEILUNG-1\user   alcwzrd.exe
        28.02.2006  14:00            17.336 VORDEFINIERT\AdministraAngler.bmp
        20.08.2009  17:44    <DIR>          VORDEFINIERT\AdministraAppPatch
        06.11.1999  16:36            40.960 LUE-ABTEILUNG-1\Foken  Binary.scr
        28.02.2006  14:00             1.272 VORDEFINIERT\AdministraBlaue Spitzen 16.bmp
        21.09.2009  11:47            80.896 LUE-ABTEILUNG-1\Foken  cadkasdeinst01.exe
        13.02.2003  12:21             7.698 LUE-ABTEILUNG-1\Foken  cadx2.ini
        14.09.2009  09:33             7.603 VORDEFINIERT\Administracfgall.ini
        28.02.2006  14:00            82.944 VORDEFINIERT\Administraclock.avi
        24.08.2009  12:08               468 LUE-ABTEILUNG-1\Foken  Cmousecc.ini
        27.03.2009  09:30               373 VORDEFINIERT\Administracmsetacl.log
        

        I think your best bet is to find some Win32 API function that gives you the required information, and invoke that function. Perhaps someone has already written a module for that, in the worst case, you need to use Win32::API or write an XS.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: How to get filesystem ownerships
by Marshall (Canon) on Apr 22, 2010 at 16:37 UTC
    #!/usr/bin/perl -w use strict; $| =1; my $path = 'C:\temp'; my @lines = `"dir /Q $path"`; foreach my $line (@lines) { next unless $line =~ m|^\d+/\d+|; my ($owner, $file) = ($line =~ /\\(\w+)\s+(.*)$/)[0,1]; print "$owner\t $file\n"; } __END__ prints: Marshall trace.pl Marshall trace.txt Marshall transpose.pl Marshall transpose1.pl ...etc...
      Argh windows perl drives me batty ..this is the output that gives me..thanks for the help though!!
      C:\temp>perl cifsplit.pl Administrators . Use of uninitialized value $owner in concatenation (.) or string at ci +fsplit.pl line 12. Use of uninitialized value $file in concatenation (.) or string at cif +split.pl l ine 12. Use of uninitialized value $owner in concatenation (.) or string at ci +fsplit.pl line 12. Use of uninitialized value $file in concatenation (.) or string at cif +split.pl l ine 12. Administrators rgreceiverInstaller.log
        Sounds like the problem lies in this regex:
        my ($owner, $file) = ($line =~ /\\(\w+)\s+(.*)$/)[0,1];
        I get Machine\User for dir /Q.
        VIPER\Marshall   Some filename with spaces.txt

        do a "dir /Q" from command line and post result here and the regex can be tweaked and yes it can be frustrating.

Re: How to get filesystem ownerships
by AR (Friar) on Apr 22, 2010 at 14:02 UTC

    Can you show us what the data looks like?

Re: How to get filesystem ownerships
by Anonymous Monk on Apr 22, 2010 at 14:18 UTC
    I am assuming the data is the output from the dir command?

    04/05/2010 08:19 AM <DIR> . 04/05/2010 08:19 AM <DIR> .. 12/30/2008 03:26 PM <DIR> .jfreereport 12/30/2008 03:26 PM <DIR> .mcm 12/02/2008 02:30 PM <DIR> .netbeans 12/02/2008 02:30 PM <DIR> .SunDownloadManager 05/27/2009 02:03 PM 2,654 balls.sam 11/17/2009 10:42 PM <DIR> Contacts 04/21/2010 03:48 PM <DIR> Desktop 12/02/2008 05:37 PM <DIR> Documents 04/19/2010 11:06 AM <DIR> Downloads 05/13/2009 09:50 AM <DIR> hpremote 11/17/2009 10:42 PM <DIR> Links 11/17/2009 10:42 PM <DIR> LocalStore 12/02/2008 02:54 PM <DIR> Music>