Previous Thread
Next Thread
Print Thread
Rate Thread
#102629 04/22/2001 12:57 AM
Joined: Apr 2001
Posts: 8
Junior Member
Junior Member
Offline
Joined: Apr 2001
Posts: 8
Ok heres the deal, I am trying to parse the 'LIST' output of any unix directory. A file entry goes something like this:

drwx------ 5 anpatel students 512 Jan 13 2000 Desktop

Simple way of getting each field is:
my @properties = split(/s+/,$line);

But this code has one specific flaw. What if the file was "New Text Document.txt", there would be no way saying that $properties[9] was the full name of the file. I thought of improving the code:

my @properties = split( /s+/, $item );
my ( $perms, $lCount, $owner, $group, $size, $mod_mon, $mod_date, $mod_time ) = @properties;
my $file = join( "", @properties[8.. $#properties] );

But $file will be "NewTextDocument.txt" because the &join will compine all the rest of array elements with "".

I was looking for a perfect solution if some one could offer, of-course i am on the hunt. But lets see if some one figures out before me wink hehehe

Thanks for reading this!

Sponsored Links
#102630 04/22/2001 1:31 AM
Joined: Sep 2000
Posts: 755
P.I.T.A. / Programmer
P.I.T.A. / Programmer
Offline
Joined: Sep 2000
Posts: 755
do you need all of the properties, or just a list of filenames?


"Annnnnnnndd now, opening for Iron Maiden...... WYLD STALLYNS!!!" --Bill S. Preston, Esquire and Ted "Theodore " Logan
#102631 04/22/2001 1:46 PM
Joined: Apr 2001
Posts: 8
Junior Member
Junior Member
Offline
Joined: Apr 2001
Posts: 8
Basically i need all the properties, but to be exact i need:

if the file is directory ($perms, $properties[0])
File Size ($size, $properties[4])
File Mod Time ( none, $properties[5... 7] )
File name ($file, $properties[8..*])

hmm i think you are suggesting me to use the simple 'ls' command, and find all the file names easily from that, I had this in mind, but dont want to use it (for many reasons)

#102632 04/22/2001 1:58 PM
Joined: Apr 2001
Posts: 8
Junior Member
Junior Member
Offline
Joined: Apr 2001
Posts: 8
hmm.. if we might locate somehow with Regexps, the char number of where the file name starts than bingo!.

But sadly i am still trying to find that way out.

#102633 04/22/2001 6:15 PM
Joined: Aug 2000
Posts: 335
Member
Member
Offline
Joined: Aug 2000
Posts: 335
Instead of trying to parse the text output of a Unix command, I'd recommend using readdir to scan through the files, and stat to get the file properties.

Sponsored Links
#102634 04/22/2001 8:27 PM
Joined: Apr 2001
Posts: 8
Junior Member
Junior Member
Offline
Joined: Apr 2001
Posts: 8
Thanks, but I am trying to parse the FTP list output, there is no way of readdir() in a FTP client, however there is a ftp->ls() command, that gives you only file name output, but there are no specific ways to merge that information to the ftp->dir() output.

The only choice that is left now is to effectively parse the LIST output.

Thanks for replying!

#102635 04/22/2001 9:01 PM
Joined: Apr 2001
Posts: 8
Junior Member
Junior Member
Offline
Joined: Apr 2001
Posts: 8
I did it!!!, i figured out that since we know at what field we need to stop, we just eliminate the fields before what we want. Sorry for bothering ya'all


my $str = "drwx------ 3 anpatel students 512 Apr 18 14:45 Really Bad Name Name for a file.";

$str =~ s/Ss+//; #1 perms-links
$str =~ s/Ss+//; #2 links-user
$str =~ s/Ss+//; #3 user-grp
$str =~ s/Ss+//; #4 grp-apr
$str =~ s/Ss+//; #5 apr-date
$str =~ s/Ss+//; #6 date-time
$str =~ s/Ss+//; #7 time-filename
$str =~ s/^(S+)s+(.*)/$2/; # remove all the things that were combined previously. result is we get the file name!
print $str;
exit;

#102636 05/09/2005 1:24 AM
Joined: Mar 2000
Posts: 21,079
Likes: 3
I type Like navaho
I type Like navaho
Joined: Mar 2000
Posts: 21,079
Likes: 3
Quote
Originally posted by Dave_L:

Instead of trying to parse the text output of a Unix command, I'd recommend using readdir to scan through the files, and stat to get the file properties.
How would stat be used after you've read the directory and would like to read/print file names, date, and size?


- Allen wavey
- What Drives You?
#102637 05/09/2005 2:48 PM
Joined: Mar 2000
Posts: 21,079
Likes: 3
I type Like navaho
I type Like navaho
Joined: Mar 2000
Posts: 21,079
Likes: 3


- Allen wavey
- What Drives You?
#102638 05/09/2005 3:10 PM
Joined: May 2000
Posts: 1,356
Addict
Addict
Joined: May 2000
Posts: 1,356
so, you no longer have a question? smile

Sponsored Links
#102639 05/09/2005 3:34 PM
Joined: Mar 2000
Posts: 21,079
Likes: 3
I type Like navaho
I type Like navaho
Joined: Mar 2000
Posts: 21,079
Likes: 3
Still have questions, but that at least tells me the reasoning behind the answer smile

I can probably write it using the example posted, I'll write back when I have questions.

Basically we readdir then use stat to be able to print the file size ($size) and date ($mtime). $filename gives me the file's name?

Would something like this be in the right direction?

Code
use File::stat;

($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = stat($filename);
then use $filename, $mtime, and $size for the data I'm looking for?


- Allen wavey
- What Drives You?
#102640 05/09/2005 3:48 PM
Joined: May 2000
Posts: 1,356
Addict
Addict
Joined: May 2000
Posts: 1,356
File::stat gives you a by-name access to stat() values (actually it returns an object). If you'll not use them, just use the CORE stat function (which returns an array)... Else, something like this will work:

Code
my @files = <*.txt>; # all txt in current dir
my $stat;
foreach my $file (@files) {
$stat = stat $file or die "Error: $!";
printf "%st%st%sn", $file, format_size($stat->size), format_time($stat->mtime);
}

#102641 05/09/2005 4:16 PM
Joined: Mar 2000
Posts: 21,079
Likes: 3
I type Like navaho
I type Like navaho
Joined: Mar 2000
Posts: 21,079
Likes: 3
thank you, that should do it smile

So in the final print line $file will give me the filename?


- Allen wavey
- What Drives You?
#102642 05/09/2005 5:07 PM
Joined: May 2000
Posts: 1,356
Addict
Addict
Joined: May 2000
Posts: 1,356
yes $file includes file name or full path to the file... depending on your usage...

#102643 05/10/2005 1:16 AM
Joined: Mar 2000
Posts: 21,079
Likes: 3
I type Like navaho
I type Like navaho
Joined: Mar 2000
Posts: 21,079
Likes: 3
How does this look?

Code
opendir(UPLOADS, "$opt{dir}/$user_number");
my @uploads = readdir(UPLOADS);
closedir(UPLOADS);
my @files = <*.txt>; # all txt in current dir
my $stat;
foreach my $file (@files) {
$stat = stat $file or die "Error: $!";

##output it

print qq(
<tr valign="top">
<td bgcolor="$vars_style{AltColumnColor2}" align="center" width="20"><input type="checkbox" name="$key" value="include" checked="checked" /></td>
<td bgcolor="$vars_style{AltColumnColor2}"><b><font size="$vars_style{FDTextSize}">$filename</font></b></td>
<td bgcolor="$vars_style{AltColumnColor2}" align="center" valign="middle">$size</td>
<td bgcolor="$vars_style{AltColumnColor2}" align="center" valign="middle">$mtime</td>
</tr>
);
}
It's not throwing an error, but then it's not reading/printing anything either tipsy

How would I fix the section to view all files, not just .txt?

Thank you again for your time smile


- Allen wavey
- What Drives You?
#102644 05/10/2005 6:52 AM
Joined: May 2000
Posts: 1,356
Addict
Addict
Joined: May 2000
Posts: 1,356
txt one was an example (or demo) on the usage smile use this:

Code
opendir(UPLOADS, "$opt{dir}/$user_number") or die "Can not opendir($opt{dir}/$user_number): $!";
my @uploads = readdir(UPLOADS);
closedir(UPLOADS);
my($size, $mtime);
foreach my $filename (@uploads) {
next if $filename =~ /^./; # ignore anything that starts with a dot. like "." ".." ".htaccess"
($size, $mtime) = (stat "$opt{dir}/$user_number/$filename")[7,9] or die "Error: $!";

##output it

print qq(
<tr valign="top">
<td bgcolor="$vars_style{AltColumnColor2}" align="center" width="20"><input type="checkbox" name="$key" value="include" checked="checked" /></td>
<td bgcolor="$vars_style{AltColumnColor2}"><b><font size="$vars_style{FDTextSize}">$filename</font></b></td>
<td bgcolor="$vars_style{AltColumnColor2}" align="center" valign="middle">$size</td>
<td bgcolor="$vars_style{AltColumnColor2}" align="center" valign="middle">$mtime</td>
</tr>
);
}
it is always better to see the actual code smile

#102645 05/10/2005 8:16 AM
Joined: Mar 2000
Posts: 21,079
Likes: 3
I type Like navaho
I type Like navaho
Joined: Mar 2000
Posts: 21,079
Likes: 3


- Allen wavey
- What Drives You?
#102646 05/10/2005 8:52 AM
Joined: May 2000
Posts: 1,356
Addict
Addict
Joined: May 2000
Posts: 1,356
no problem... but you need to swap "Date:" "Size:" texts smile and... I can give you a byte converter smile

Code
sub fsize {
my $size = shift or return '0 byte';
return sprintf("%1.2f %s",$size / 1024 / 1024 / 1024, " GB" ) if($size >= 1024*1024*1024);
return sprintf("%1.2f %s",$size / 1024 / 1024 , " MB" ) if($size >= 1024*1024 );
return sprintf("%1.2f %s",$size / 1024 , " KB" ) if($size >= 1024 );
return sprintf("%1.2f %s",$size , " Byte") if($size < 1024 );
return "$size Byte";
}
so, you can use it like:

Code
opendir(UPLOADS, "$opt{dir}/$user_number") or die "Can not opendir($opt{dir}/$user_number): $!";
my @uploads = readdir(UPLOADS);
closedir(UPLOADS);
my($size, $mtime);
foreach my $filename (@uploads) {
next if $filename =~ /^./; # ignore anything that starts with a dot. like "." ".." ".htaccess"
next if -d "$opt{dir}/$user_number/$filename"; # ignore directories
($size, $mtime) = (stat "$opt{dir}/$user_number/$filename")[7,9] or die "Error: $!";

##output it
$size = fsize($size);

print qq(
<tr valign="top">
<td bgcolor="$vars_style{AltColumnColor2}" align="center" width="20"><input type="checkbox" name="$key" value="include" checked="checked" /></td>
<td bgcolor="$vars_style{AltColumnColor2}"><b><font size="$vars_style{FDTextSize}">$filename</font></b></td>
<td bgcolor="$vars_style{AltColumnColor2}" align="center" valign="middle">$size</td>
<td bgcolor="$vars_style{AltColumnColor2}" align="center" valign="middle">$mtime</td>
</tr>
);
}

#102647 05/10/2005 8:57 AM
Joined: May 2000
Posts: 1,356
Addict
Addict
Joined: May 2000
Posts: 1,356
hey! I've just noticed the cake! laugh Happy Birthday! thumbsup

#102648 05/10/2005 11:18 AM
Joined: Mar 2000
Posts: 21,079
Likes: 3
I type Like navaho
I type Like navaho
Joined: Mar 2000
Posts: 21,079
Likes: 3
grazi, I'm not getting older, I'm getting... well, ok, I'm getting older tipsy


- Allen wavey
- What Drives You?
#102649 09/15/2005 8:40 AM
Joined: Sep 2005
Posts: 1
Junior Member
Junior Member
Offline
Joined: Sep 2005
Posts: 1
May be this looks a bit more appropriate

my $str = "drwx------ 3 anpatel students 512 Apr 18 14:45 Really Bad Name Name for a file.";

$str =~ s/(S)s/$1/; #1 perms-links
$str =~ s/(S)s/$1/; #2 links-user
$str =~ s/(S)s/$1/; #3 user-grp
$str =~ s/(S)s/$1/; #4 grp-apr
$str =~ s/(S)s/$1/; #5 apr-date
$str =~ s/(S)s/$1/; #6 date-time
$str =~ s/(S)s/$1/; #7 time-filename
$str =~ s/^(S+)s+(.*)/$2/; # remove all the things that were combined previously. result is we get the file name!
print $str,"n";


Link Copied to Clipboard
Donate Today!
Donate via PayPal

Donate to UBBDev today to help aid in Operational, Server and Script Maintenance, and Development costs.

Please also see our parent organization VNC Web Services if you're in the need of a new UBB.threads Install or Upgrade, Site/Server Migrations, or Security and Coding Services.
Recommended Hosts
We have personally worked with and recommend the following Web Hosts:
Stable Host
bluehost
InterServer
Visit us on Facebook
Member Spotlight
isaac
isaac
California
Posts: 1,157
Joined: July 2001
Forum Statistics
Forums63
Topics37,573
Posts293,925
Members13,849
Most Online5,166
Sep 15th, 2019
Today's Statistics
Currently Online
Topics Created
Posts Made
Users Online
Birthdays
Top Posters
AllenAyres 21,079
JoshPet 10,369
LK 7,394
Lord Dexter 6,708
Gizmo 5,833
Greg Hard 4,625
Top Posters(30 Days)
Top Likes Received
isaac 82
Gizmo 20
Brett 7
WebGuy 2
Top Likes Received (30 Days)
None yet
The UBB.Developers Network (UBB.Dev/Threads.Dev) is ©2000-2024 VNC Web Services

 
Powered by UBB.threads™ PHP Forum Software 8.0.0
(Preview build 20221218)