Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   parsing file name assigning extension to a variable (http://www.velocityreviews.com/forums/t886832-parsing-file-name-assigning-extension-to-a-variable.html)

Alexander Heimann 06-11-2004 12:01 AM

parsing file name assigning extension to a variable
 
Hi guys. I am new to Perl(four days). I am having a blast playing with
it. There seems to be a hundred ways to solve each problem I am faced
with.
Currently I am working on migrating data to a new database. I need to
read the contents of a few thousand files and then insert the contents
into a database. The trick is the files are named desc.121655 with
121655 being the record number or primary key in the database. So I
need to parse the filename and save the extension to a variable to
later use in my SQL statement. The steps I think i need to take are
below any comments would be great

1. open directory..
2. go file by file
3 assign extension of file to a variable @recordNum
4 assign contents of file to a variable @content
5 then insert content with SQL statement where PK = @recordNum
6 then do next file until end of directory

sorry if that sounds confusing i am a bit new at this

Alex

gnari 06-11-2004 12:12 AM

Re: parsing file name assigning extension to a variable
 
"Alexander Heimann" <AlexanderHeimann@yahoo.com> wrote in message
news:1c63154d.0406101601.29f1448f@posting.google.c om...

[snip problem without actual question]

> 1. open directory..
> 2. go file by file
> 3 assign extension of file to a variable @recordNum


$recordNum ?

> 4 assign contents of file to a variable @content


$content ?

> 5 then insert content with SQL statement where PK = @recordNum


ditto

> 6 then do next file until end of directory


sounds good. go for it and let us know how it goes.

gnari




Sam Holden 06-11-2004 12:19 AM

Re: parsing file name assigning extension to a variable
 
On 10 Jun 2004 17:01:28 -0700,
Alexander Heimann <AlexanderHeimann@yahoo.com> wrote:
> Hi guys. I am new to Perl(four days). I am having a blast playing with
> it. There seems to be a hundred ways to solve each problem I am faced
> with.
> Currently I am working on migrating data to a new database. I need to
> read the contents of a few thousand files and then insert the contents
> into a database. The trick is the files are named desc.121655 with
> 121655 being the record number or primary key in the database. So I
> need to parse the filename and save the extension to a variable to
> later use in my SQL statement. The steps I think i need to take are
> below any comments would be great
>
> 1. open directory..


perldoc -f opendir

> 2. go file by file


perldoc -f readdir
perldoc perlsyn [look for for, foreach, while]

> 3 assign extension of file to a variable @recordNum


perldoc File::Basename

You certainly don't want to use an array for a single extension
(and you don't seem to need to keep all the data at once)

> 4 assign contents of file to a variable @content


perldoc -f open
perldoc -f readline
perldoc -f read
perldoc -f close

> 5 then insert content with SQL statement where PK = @recordNum


perldoc DBI

> 6 then do next file until end of directory


'}'


perldoc is a command on most perl installs to read the documentation you
may have it available as HTML or in some other format, in which case
"perldoc -f foo" means the foo function documented in the perlfunc
documentation. "perldoc File::Basename" means the documentation for
the File::Basename module. perldoc perlsyn means the perlsyn
documentation.

--
Sam Holden

John Bokma 06-11-2004 12:54 AM

Re: parsing file name assigning extension to a variable
 
Sam Holden wrote:

> On 10 Jun 2004 17:01:28 -0700,
> Alexander Heimann <AlexanderHeimann@yahoo.com> wrote:
>
>>Hi guys. I am new to Perl(four days). I am having a blast playing with
>>it. There seems to be a hundred ways to solve each problem I am faced
>>with.
>>Currently I am working on migrating data to a new database. I need to
>>read the contents of a few thousand files and then insert the contents
>>into a database. The trick is the files are named desc.121655 with
>>121655 being the record number or primary key in the database. So I
>>need to parse the filename and save the extension to a variable to
>>later use in my SQL statement. The steps I think i need to take are
>>below any comments would be great
>>
>>1. open directory..

>
> perldoc -f opendir
>
>>2. go file by file

>
>
> perldoc -f readdir
> perldoc perlsyn [look for for, foreach, while]


Or File::Find

>>3 assign extension of file to a variable @recordNum

>
> perldoc File::Basename
>
> You certainly don't want to use an array for a single extension
> (and you don't seem to need to keep all the data at once)
>
>>4 assign contents of file to a variable @content

>
> perldoc -f open
> perldoc -f readline
> perldoc -f read
> perldoc -f close


File::Slurp (you probably have to install that one, see ppm)

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html

Tore Aursand 06-11-2004 10:24 AM

Re: parsing file name assigning extension to a variable
 
On Thu, 10 Jun 2004 19:54:58 -0500, John Bokma wrote:
> [...]
> Or File::Find


....or File::Find::Rule, which I find a lot easier to work with. :)


--
Tore Aursand <tore@aursand.no>

John Bokma 06-11-2004 10:49 AM

Re: parsing file name assigning extension to a variable
 
Tore Aursand wrote:

> On Thu, 10 Jun 2004 19:54:58 -0500, John Bokma wrote:
>
>>[...]
>>Or File::Find

>
> ...or File::Find::Rule, which I find a lot easier to work with. :)


I will have a look into that one, thanks.

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html

Alexander Heimann 06-11-2004 03:07 PM

Re: parsing file name assigning extension to a variable
 
"gnari" <gnari@simnet.is> wrote in message news:<caat96$7ne$1@news.simnet.is>...
> "Alexander Heimann" <AlexanderHeimann@yahoo.com> wrote in message
> news:1c63154d.0406101601.29f1448f@posting.google.c om...
>
> [snip problem without actual question]
>
> > 1. open directory..
> > 2. go file by file
> > 3 assign extension of file to a variable @recordNum

>
> $recordNum ?
>
> > 4 assign contents of file to a variable @content

>
> $content ?
>
> > 5 then insert content with SQL statement where PK = @recordNum

>
> ditto
>
> > 6 then do next file until end of directory

>
> sounds good. go for it and let us know how it goes.
>
> gnari

thanks everyone for all your help. i will let you guys know how it goes.

have an awesome weekend...

Randal L. Schwartz 06-11-2004 03:56 PM

Re: parsing file name assigning extension to a variable
 
>>>>> "Tore" == Tore Aursand <tore@aursand.no> writes:

Tore> On Thu, 10 Jun 2004 19:54:58 -0500, John Bokma wrote:
>> [...]
>> Or File::Find


Tore> ...or File::Find::Rule, which I find a lot easier to work with. :)

.... or File::Finder, which is at least two characters shorter. :)

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

John Bokma 06-11-2004 05:25 PM

Re: parsing file name assigning extension to a variable
 
Randal L. Schwartz wrote:

>>>>>>"Tore" == Tore Aursand <tore@aursand.no> writes:

>
> Tore> On Thu, 10 Jun 2004 19:54:58 -0500, John Bokma wrote:
>
>>>[...]
>>>Or File::Find

>
>
> Tore> ...or File::Find::Rule, which I find a lot easier to work with. :)
>
> ... or File::Finder, which is at least two characters shorter. :)


More on my list to check out :-D. Thanks.

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html

Alexander Heimann 06-14-2004 08:35 PM

Re: parsing file name assigning extension to a variable
 
maybe someone can tell me why I am unable to read the file when i do
each step individually it was working but i am having trouble putting
it all together..


use File::Basename;
fileparse_set_fstype("MSDOS");


opendir (DIR, "D:/D2") or die "couldn't open directory\n";
while (defined($file = readdir(DIR))) {



($name, $dir, $ext) = fileparse($file, '\..*');
$ext =~s/^\.//;
print " dir is $dir, name is $name, extension is $ext\n";

my $input;
open($input, "<", "$file")
#or die "Couldn't open file :!\n";
while(<$input>){
undef $/;
$content = <INPUT>;
print if /of/;

print $content;


}
close($input);







}


closedir DIR;


All times are GMT. The time now is 01:13 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.