regular expression for beow text

K

king

I have a below text like below.
"
Volume in drive C has no label.
Volume Serial Number is 4496-C1CF

Directory of C:\Documents and Settings\Administrator\Desktop\test

08/20/2010 10:01 PM 0 New WinZip File.zip
08/20/2010 10:01 PM 0 New WinZip File (2).zip
08/20/2010 10:01 PM 0 New WinZip File (3).zip
08/20/2010 11:10 PM 11,226 Test_1.txt
08/21/2010 01:56 AM 174 test_2.txt
5 File(s) 11,400 bytes
0 Dir(s) 20,658,462,720 bytes free"

I want to have atwo regular expressions where in I will get no of
files as in 5
and the last file name i.e test_2.txt.

I tried the below thing..but I am getting the whole line as out put. I
just need the number 5 as out put and the name of the last modified
file only.
^(\s+)(\d+)(\s)(Fie*)

Can anybody help me with the regular expression?
 
S

sln

I have a below text like below.
"
Volume in drive C has no label.
Volume Serial Number is 4496-C1CF

Directory of C:\Documents and Settings\Administrator\Desktop\test

08/20/2010 10:01 PM 0 New WinZip File.zip
08/20/2010 10:01 PM 0 New WinZip File (2).zip
08/20/2010 10:01 PM 0 New WinZip File (3).zip
08/20/2010 11:10 PM 11,226 Test_1.txt
08/21/2010 01:56 AM 174 test_2.txt
5 File(s) 11,400 bytes
0 Dir(s) 20,658,462,720 bytes free"

I want to have atwo regular expressions where in I will get no of
files as in 5
and the last file name i.e test_2.txt.

I tried the below thing..but I am getting the whole line as out put. I
just need the number 5 as out put and the name of the last modified
file only.
^(\s+)(\d+)(\s)(Fie*)

Can anybody help me with the regular expression?

There are a lot of output options for the dir command.
Try 'dir \B'

Used your way:
/^[\d\/]+ \s+ [\d:]+ \s+ [APM]+ \s+ [\d,]+ \s+ ([^\n]+) /x

-sln
 
J

Jim Gibson

king said:
I have a below text like below.
"
Volume in drive C has no label.
Volume Serial Number is 4496-C1CF

Directory of C:\Documents and Settings\Administrator\Desktop\test

08/20/2010 10:01 PM 0 New WinZip File.zip
08/20/2010 10:01 PM 0 New WinZip File (2).zip
08/20/2010 10:01 PM 0 New WinZip File (3).zip
08/20/2010 11:10 PM 11,226 Test_1.txt
08/21/2010 01:56 AM 174 test_2.txt
5 File(s) 11,400 bytes
0 Dir(s) 20,658,462,720 bytes free"

I want to have atwo regular expressions where in I will get no of
files as in 5
and the last file name i.e test_2.txt.

I tried the below thing..but I am getting the whole line as out put. I
just need the number 5 as out put and the name of the last modified
file only.
^(\s+)(\d+)(\s)(Fie*)

Can anybody help me with the regular expression?

That regular expression will work (provided you correct your spelling
error) if applied to the correct line. The number '5' should be in the
variable $2 after that regular expression is applied to the correct
line. The reason that you are "getting the whole line as output" cannot
be explained because you do not show us your program.

Please post a short-but-complete program that illustrates the problem
you are having. If you do that, someone can surely help you.

Thanks.
 
S

Scott Bryce

king said:
I have a below text like below.
"
Volume in drive C has no label.
Volume Serial Number is 4496-C1CF

Directory of C:\Documents and Settings\Administrator\Desktop\test

08/20/2010 10:01 PM 0 New WinZip File.zip
08/20/2010 10:01 PM 0 New WinZip File (2).zip
08/20/2010 10:01 PM 0 New WinZip File (3).zip
08/20/2010 11:10 PM 11,226 Test_1.txt
08/21/2010 01:56 AM 174 test_2.txt
5 File(s) 11,400 bytes
0 Dir(s) 20,658,462,720 bytes free"

I want to have atwo regular expressions where in I will get no of
files as in 5 and the last file name i.e test_2.txt.

I tried the below thing..but I am getting the whole line as out put.
I just need the number 5 as out put and the name of the last modified
file only. ^(\s+)(\d+)(\s)(Fie*)

Can anybody help me with the regular expression?

It would be helpful if you posted an actual script that we could run,
rather than just a regex that looks like it was retyped, rather than
copied and pasted. You are matching the line you want, but you don't
show us what you do with that match the "gets" you the whole line, or
how you attempt to isolate the file count.

Data in nice, neat columns begs to be split. Do you need a regex, or do
you need to isolate the number of files and the name of the last file
listed?

Post a complete script that demonstrates your problem, and someone here
can help you fix it.
 
S

sln

Please post Real Perl Code that does not generate warnings.

Have you seen the Posting Guidelines that are posted here frequently?




Sorry I could not help you, but just in case anybody else is
reading this and would be willing to accept one regular expression,
I'll throw that out there...




---------------------------------------
#!/usr/bin/perl
use warnings;
use strict;

$_ =
'
Volume in drive C has no label.
Volume Serial Number is 4496-C1CF

Directory of C:\Documents and Settings\Administrator\Desktop\test

08/20/2010 10:01 PM 0 New WinZip File.zip
08/20/2010 10:01 PM 0 New WinZip File (2).zip
08/20/2010 10:01 PM 0 New WinZip File (3).zip
08/20/2010 11:10 PM 11,226 Test_1.txt
08/21/2010 01:56 AM 174 test_2.txt
5 File(s) 11,400 bytes
0 Dir(s) 20,658,462,720 bytes free';

my($fname, $fcount) = /(\S+)\s+(\d+)\s+\QFile(s)/;
print "$fname, $fcount\n";
---------------------------------------


Note that it will fail horribly if you happen to have a
filename that starts with "File(s)" as in:

08/20/2010 10:01 PM 8 File(s)OfStuff.zip

This wouldn't be terribly good either:

08/20/2010 10:01 PM 0 New WinZip File.zip
08/20/2010 10:01 PM 0 New WinZip File (2).zip
08/20/2010 10:01 PM 0 New WinZip File (3).zip
08/20/2010 11:10 PM 11,226 Test_1.txt
08/21/2010 01:56 AM <DIR> test_2.txt
4 File(s) 11,400 bytes
1 Dir(s) 20,658,462,720 bytes free';

-sln
 
K

king

This wouldn't be terribly good either:

08/20/2010  10:01 PM                 0 New WinZip File.zip
08/20/2010  10:01 PM                 0 New WinZip File (2).zip
08/20/2010  10:01 PM                 0 New WinZip File (3).zip
08/20/2010  11:10 PM            11,226 Test_1.txt
08/21/2010  01:56 AM    <DIR>          test_2.txt
               4 File(s)         11,400 bytes
               1 Dir(s)  20,658,462,720 bytes free';

-sln

Hi,
Well what I am trying to do is somehting as below:
My script:
#!/c/perl/bin
open(SYSCMD, "dir /A:A /O:D |");
while(<SYSCMD>)
{
#if(/^(\s+)(\d)\s/)
#if(/^(\s+)(\d+)\s/)
if(/^[\d\/]+\s+[\d:]+\s+[APM]+\s+[\d,]+\s+([^\n]+)/)
{
$count = $_;
}
}
close(SYSCMD);
print "$count\n";
Similarly I want the last modified or the latest file name and do some
operation with that file.
I will keep count in a text file and next time when i run, if count is
more, will copy that latest file from that folder.

Now I am not getting a regular expression to get the count of the file
and the latest file that is updated in the folder.

Hope this explians what exactly I want to do.
PLease let me know, if anybody can help me with the regular expression
for the latest file name from command out put and the no of file count.
 
S

Scott Bryce

king said:
Hi,
Well what I am trying to do is somehting as below:
My script:
#!/c/perl/bin
open(SYSCMD, "dir /A:A /O:D |");
while(<SYSCMD>)
{
#if(/^(\s+)(\d)\s/)
#if(/^(\s+)(\d+)\s/)
if(/^[\d\/]+\s+[\d:]+\s+[APM]+\s+[\d,]+\s+([^\n]+)/)
{
$count = $_;
}
}
close(SYSCMD);
print "$count\n";

Somehow I thought you were reading this in line by line. While I like
Ben Morrow's solution better, I came up with this:

----------------

use strict;
use warnings;

my $last_file_name;
my $file_count;

while (<DATA>)
{
chomp;
next if ! $_;

my @columns = split ' ', $_, 5;

next if $columns[0] eq 'Volume';
next if $columns[0] eq 'Directory';
next if $columns[3] eq '<DIR>';

if ($columns[1] eq 'File(s)')
{
$file_count = $columns[0];
last;
}

$last_file_name = $columns[4];
}

print "There are $file_count files.\nThe last file listed is:
$last_file_name\n";

__DATA__
Volume in drive C has no label.
Volume Serial Number is 4496-C1CF

Directory of C:\Documents and Settings\Administrator\Desktop\test

08/20/2010 10:01 PM 0 New WinZip File.zip
08/20/2010 10:01 PM 0 New WinZip File (2).zip
08/20/2010 10:01 PM 0 New WinZip File (3).zip
08/20/2010 11:10 PM 11,226 Test_1.txt
08/21/2010 01:56 AM 174 test_2.txt
08/21/2010 01:56 AM <DIR> test_3.txt
5 File(s) 11,400 bytes
0 Dir(s) 20,658,462,720 bytes free

----------------
I will keep count in a text file and next time when i run, if count
is more, will copy that latest file from that folder.

Just the latest, or any file created since the last run of the script?
 
S

sln

Hi,
Well what I am trying to do is somehting as below:
My script:
#!/c/perl/bin
open(SYSCMD, "dir /A:A /O:D |");
while(<SYSCMD>)
{
#if(/^(\s+)(\d)\s/)
#if(/^(\s+)(\d+)\s/)
if(/^[\d\/]+\s+[\d:]+\s+[APM]+\s+[\d,]+\s+([^\n]+)/)
{
$count = $_;
}
}
close(SYSCMD);
print "$count\n";

I think you would get better results if you include all
files (and directories), the /A:A is going to exclude some files.
:\> dir /O:D is all thats needed, the latest is at the bottom.

-sln
----------------------

use strict;
use warnings;

my ($cnt, $last_file) = (0, '');
open(SYSCMD, "dir /O:D |") or die "open pipe failed: $!";

while (<SYSCMD>) {
if (/^[\d\/]+ \s+ [\d:]+ \s+ [APM]+ \s+ [\d,]+ \s+ ([^\n]+) /x) {
$cnt++;
$last_file = $1;
}
}
close(SYSCMD);

print "$cnt '$last_file'";
 
K

king

Hi,
Well what I am trying to do is somehting as below:
My script:
#!/c/perl/bin
open(SYSCMD, "dir /A:A /O:D |");
while(<SYSCMD>)
{
   #if(/^(\s+)(\d)\s/)
   #if(/^(\s+)(\d+)\s/)
   if(/^[\d\/]+\s+[\d:]+\s+[APM]+\s+[\d,]+\s+([^\n]+)/)
   {
           $count = $_;
   }
}
close(SYSCMD);
print "$count\n";

I think you would get better results if you include all
files (and directories), the /A:A is going to exclude some files.
 :\> dir /O:D is all thats needed, the latest is at the bottom.

-sln
Thanks a lot for this..this solution works perfectly.

Regards
use warnings;

my ($cnt, $last_file) = (0, '');
open(SYSCMD, "dir /O:D |") or die "open pipe failed: $!";

while (<SYSCMD>) {
    if (/^[\d\/]+ \s+ [\d:]+ \s+ [APM]+ \s+ [\d,]+ \s+ ([^\n]+) /x) {
        $cnt++;
        $last_file = $1;
    }}

close(SYSCMD);

print "$cnt   '$last_file'";
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,001
Latest member
Kendra00E1

Latest Threads

Top