php forum
php mysql forum
php mysql smarty
 
Page 1 of 2 1 2 >
Topic Options
#102629 - 04/22/01 12:57 AM Regular Expressions
HelloWorld Offline
Junior Member

Registered: 04/19/01
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!

Top
#102630 - 04/22/01 01:31 AM Re: Regular Expressions
Mark Badolato Offline
P.I.T.A. / Programmer

Registered: 09/08/00
Posts: 836
Loc: Land O' Cactus
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

Top
#102631 - 04/22/01 01:46 PM Re: Regular Expressions
HelloWorld Offline
Junior Member

Registered: 04/19/01
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)

Top
#102632 - 04/22/01 01:58 PM Re: Regular Expressions
HelloWorld Offline
Junior Member

Registered: 04/19/01
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.

Top
#102633 - 04/22/01 06:15 PM Re: Regular Expressions
Dave_L Offline
Member

Registered: 08/16/00
Posts: 356
Loc: Virginia, USA
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.

Top
#102634 - 04/22/01 08:27 PM Re: Regular Expressions
HelloWorld Offline
Junior Member

Registered: 04/19/01
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!

Top
#102635 - 04/22/01 09:01 PM Re: Regular Expressions
HelloWorld Offline
Junior Member

Registered: 04/19/01
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;

Top
#102636 - 05/09/05 01:24 AM Re: Regular Expressions
AllenAyres Offline

I type Like navaho

Registered: 03/10/00
Posts: 25580
Loc: Texas
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?

Top
#102637 - 05/09/05 02:48 PM Re: Regular Expressions
AllenAyres Offline

I type Like navaho

Registered: 03/10/00
Posts: 25580
Loc: Texas
_________________________
- Allen wavey
- What Drives You?

Top
#102638 - 05/09/05 03:10 PM Re: Regular Expressions
Burak Offline
Addict

Registered: 05/29/00
Posts: 1820
Loc: Istanbul, Turkey
so, you no longer have a question? smile
_________________________
one bytecode to rule them all

Top
#102639 - 05/09/05 03:34 PM Re: Regular Expressions
AllenAyres Offline

I type Like navaho

Registered: 03/10/00
Posts: 25580
Loc: Texas
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?

Top
#102640 - 05/09/05 03:48 PM Re: Regular Expressions
Burak Offline
Addict

Registered: 05/29/00
Posts: 1820
Loc: Istanbul, Turkey
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);
}
_________________________
one bytecode to rule them all

Top
#102641 - 05/09/05 04:16 PM Re: Regular Expressions
AllenAyres Offline

I type Like navaho

Registered: 03/10/00
Posts: 25580
Loc: Texas
thank you, that should do it smile

So in the final print line $file will give me the filename?
_________________________
- Allen wavey
- What Drives You?

Top
#102642 - 05/09/05 05:07 PM Re: Regular Expressions
Burak Offline
Addict

Registered: 05/29/00
Posts: 1820
Loc: Istanbul, Turkey
yes $file includes file name or full path to the file... depending on your usage...
_________________________
one bytecode to rule them all

Top
#102643 - 05/10/05 01:16 AM Re: Regular Expressions
AllenAyres Offline

I type Like navaho

Registered: 03/10/00
Posts: 25580
Loc: Texas
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?

Top
#102644 - 05/10/05 06:52 AM Re: Regular Expressions
Burak Offline
Addict

Registered: 05/29/00
Posts: 1820
Loc: Istanbul, Turkey
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
_________________________
one bytecode to rule them all

Top
#102645 - 05/10/05 08:16 AM Re: Regular Expressions
AllenAyres Offline

I type Like navaho

Registered: 03/10/00
Posts: 25580
Loc: Texas
_________________________
- Allen wavey
- What Drives You?

Top
#102646 - 05/10/05 08:52 AM Re: Regular Expressions
Burak Offline
Addict

Registered: 05/29/00
Posts: 1820
Loc: Istanbul, Turkey
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>
	);
}
_________________________
one bytecode to rule them all

Top
#102647 - 05/10/05 08:57 AM Re: Regular Expressions
Burak Offline
Addict

Registered: 05/29/00
Posts: 1820
Loc: Istanbul, Turkey
hey! I've just noticed the cake! laugh Happy Birthday! thumbsup
_________________________
one bytecode to rule them all

Top
#102648 - 05/10/05 11:18 AM Re: Regular Expressions
AllenAyres Offline

I type Like navaho

Registered: 03/10/00
Posts: 25580
Loc: Texas
grazi, I'm not getting older, I'm getting... well, ok, I'm getting older tipsy
_________________________
- Allen wavey
- What Drives You?

Top
#102649 - 09/15/05 08:40 AM Re: Regular Expressions
BLESSY JOSE Offline
Junior Member

Registered: 09/15/05
Posts: 1
Loc: Bangalore,India
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";

Top
Page 1 of 2 1 2 >



Latest Posts
[7.2.1] - Naked shoutbox
by bellaonline
05/05/12 05:00 PM
[7.x] Stop Forum Spam Integration v0.4
by bellaonline
05/05/12 03:53 PM
Shout Box

(Views)Popular Topics
Known public proxy servers 1689885
Integrated Index Page (IIP) 5.3.1 555705
Finished-[6.5.2] Games Arcade Deluxe v1.9 501236
Integrated Index Page (IIP) 5.1.1 415112
TLD Bv2.1 Released - Threads Links Directory 396822
[6.0x] Who's Online 4.0.0 [Finished] 389412
Finished-[6.5.1] Integrated Index Page (IIP) 6.5 330423
Q & A 298663
Slash UBB 266936
[6.3.x] [beta] Hit Hack 2.0 227970
Forum Stats
13621 Members
59 Forums
37191 Topics
295716 Posts

Max Online: 686 @ 06/28/07 07:04 AM

 

 

 
fusionbb message board php hacks