Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > parsing file name assigning extension to a variable

Reply
Thread Tools

parsing file name assigning extension to a variable

 
 
Alexander Heimann
Guest
Posts: n/a
 
      06-11-2004
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
 
Reply With Quote
 
 
 
 
gnari
Guest
Posts: n/a
 
      06-11-2004
"Alexander Heimann" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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



 
Reply With Quote
 
 
 
 
Sam Holden
Guest
Posts: n/a
 
      06-11-2004
On 10 Jun 2004 17:01:28 -0700,
Alexander Heimann <(E-Mail Removed)> 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
 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      06-11-2004
Sam Holden wrote:

> On 10 Jun 2004 17:01:28 -0700,
> Alexander Heimann <(E-Mail Removed)> 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
 
Reply With Quote
 
Tore Aursand
Guest
Posts: n/a
 
      06-11-2004
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 <(E-Mail Removed)>
 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      06-11-2004
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
 
Reply With Quote
 
Alexander Heimann
Guest
Posts: n/a
 
      06-11-2004
"gnari" <(E-Mail Removed)> wrote in message news:<caat96$7ne$(E-Mail Removed)>...
> "Alexander Heimann" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) 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...
 
Reply With Quote
 
Randal L. Schwartz
Guest
Posts: n/a
 
      06-11-2004
>>>>> "Tore" == Tore Aursand <(E-Mail Removed)> 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
<(E-Mail Removed)> <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!
 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      06-11-2004
Randal L. Schwartz wrote:

>>>>>>"Tore" == Tore Aursand <(E-Mail Removed)> 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 . 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
 
Reply With Quote
 
Alexander Heimann
Guest
Posts: n/a
 
      06-14-2004
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;
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Variable variable name" or "variable lvalue" mfglinux Python 11 09-12-2007 03:08 AM
adding a variable name to a hash to name is part of the variable name Bobby Chamness Perl 2 04-22-2007 09:54 PM
Assigning methods to objects, and assigning onreadystatechange to an XMLHttpRequest -- an inconsistency? weston Javascript 1 09-22-2006 09:33 AM
if instance variable get initialize after assigning some values or after constructor then when does static variable get initialize Tony Morris Java 3 02-04-2006 08:39 AM
document.getElementById fails when assigning return value to variable with same name as id? weston Javascript 19 01-01-2006 08:22 PM



Advertisments