Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: parsing the Windows directories

by dasgar (Deacon)
on Aug 20, 2014 at 17:03 UTC ( #1098134=note: print w/ replies, xml ) Need Help??


in reply to parsing the Windows directories

Building off comments from others, here's something that I came up that I think does what you're wanting.

use strict; use warnings; use feature 'say'; use File::Find::Rule; my $dir = 'C:\testdir'; if ($#ARGV >= 0) {$dir = $ARGV[0];} my $rule = File::Find::Rule->new; my @items = $rule->in($dir); my @fields = ("dev","ino","mode","nlink","uid","gid","rdev","size","at +ime", "mtime","ctime","blksize","blocks"); my %num; my $index = 0; foreach my $field (@fields) { $num{$field} = $index; $index++; } foreach my $item (@items) { my $type = (-f $item) ? 'file' : (-l $item) ? 'symbolic link' : (-d $item) ? 'directory' : (-b $item) ? 'block special file' : (-c $item) ? 'character special file' : (-p $item) ? 'named pipe' : (-S $item) ? 'socket' : 'unkown'; my (@lstat) = lstat $item; my %times; foreach my $time ("atime","ctime","mtime") { $times{$time} = localtime($lstat[$num{$time}]); } my $size = commify($lstat[$num{'size'}]); my $perms = sprintf "%04o", $lstat[$num{'mode'}] & 07777; $item =~ s|\/|\\|g; say "Found item '$item'"; say " Type: $type"; say " Permissions: $perms"; say " Size: $size bytes"; say " Access Time: $times{'atime'}"; say " Inode change time: $times{'ctime'}"; say " Last modify time: $times{'mtime'}"; say "\n"; } sub commify { my $input = shift; $input = reverse $input; $input =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g; return reverse $input; }

Below is a sample output from the code above. There are two Windows shortcuts to cmd.exe included in the output, which did not cause any problems. It kind of looks like you were trying to parse through the folder that holds the "Favorites" from Internet Explorer. I tested the script above against the Favorites folder on my system and I didn't encounter any issues.

Found item 'C:\testdir' Type: directory Permissions: 0777 Size: 0 bytes Access Time: Wed Aug 20 10:49:37 2014 Inode change time: Wed Aug 20 10:47:33 2014 Last modify time: Wed Aug 20 10:49:37 2014 Found item 'C:\testdir\cmd.lnk' Type: file Permissions: 0666 Size: 1,197 bytes Access Time: Wed Aug 20 10:48:44 2014 Inode change time: Wed Aug 20 10:48:44 2014 Last modify time: Wed Aug 20 10:49:37 2014 Found item 'C:\testdir\test1.txt' Type: file Permissions: 0666 Size: 14 bytes Access Time: Wed Aug 20 10:48:08 2014 Inode change time: Wed Aug 20 10:48:08 2014 Last modify time: Wed Aug 20 10:50:15 2014 Found item 'C:\testdir\test2.txt' Type: file Permissions: 0666 Size: 17 bytes Access Time: Wed Aug 20 10:48:20 2014 Inode change time: Wed Aug 20 10:48:08 2014 Last modify time: Wed Aug 20 10:50:24 2014 Found item 'C:\testdir\dir1' Type: directory Permissions: 0777 Size: 0 bytes Access Time: Wed Aug 20 10:50:30 2014 Inode change time: Wed Aug 20 10:48:33 2014 Last modify time: Wed Aug 20 10:50:30 2014 Found item 'C:\testdir\dir1\cmd.lnk' Type: file Permissions: 0666 Size: 1,197 bytes Access Time: Wed Aug 20 10:50:30 2014 Inode change time: Wed Aug 20 10:50:30 2014 Last modify time: Wed Aug 20 10:49:37 2014 Found item 'C:\testdir\dir1\test1.txt' Type: file Permissions: 0666 Size: 14 bytes Access Time: Wed Aug 20 10:50:30 2014 Inode change time: Wed Aug 20 10:50:30 2014 Last modify time: Wed Aug 20 10:50:15 2014 Found item 'C:\testdir\dir1\test2.txt' Type: file Permissions: 0666 Size: 17 bytes Access Time: Wed Aug 20 10:50:30 2014 Inode change time: Wed Aug 20 10:50:30 2014 Last modify time: Wed Aug 20 10:50:24 2014


Comment on Re: parsing the Windows directories
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1098134]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2014-12-29 11:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (186 votes), past polls