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

 
 
Paul Lalli
Guest
Posts: n/a
 
      06-14-2004
On Mon, 14 Jun 2004, Alexander Heimann wrote:

> 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..


You've left off at least two vital pieces of information, necessary for
anyone to effectively help you:

1) What is your desired goal and/or output?
2) What is the result / output of the code you tried? (this includes all
errors and warnings that may be printed).

Paul Lalli

>
>
> 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
 
 
 
 
Alexander Heimann
Guest
Posts: n/a
 
      06-15-2004
Paul Lalli <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)> ...
> On Mon, 14 Jun 2004, Alexander Heimann wrote:
>
> > 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..

>
> You've left off at least two vital pieces of information, necessary for
> anyone to effectively help you:
>
> 1) What is your desired goal and/or output?
> 2) What is the result / output of the code you tried? (this includes all
> errors and warnings that may be printed).
>
> Paul Lalli

Paul, My apologies.
1) My desired goal and output
1. open directory..
2. go file by file
3 assign extension of file to a variable @ext
4 assign contents of file to a variable $content
5 then insert content with SQL statement where PK
6 then do next file until end of directory
2) I am getting the die error output when trying to read the file. The
code parses the filename fine when i comment out the open file portion
 
Reply With Quote
 
 
 
 
gnari
Guest
Posts: n/a
 
      06-15-2004
"Alexander Heimann" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 1) My desired goal and output
> 1. open directory..
> 2. go file by file
> 3 assign extension of file to a variable @ext
> 4 assign contents of file to a variable $content
> 5 then insert content with SQL statement where PK
> 6 then do next file until end of directory
> 2) I am getting the die error output when trying to read the file. The
> code parses the filename fine when i comment out the open file portion


you obviously are forgetting the directory part of the
filename when opening it

gnari



 
Reply With Quote
 
Alexander Heimann
Guest
Posts: n/a
 
      06-15-2004
gnari wrote:
> "Alexander Heimann" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
>
>>1) My desired goal and output
>>1. open directory..
>>2. go file by file
>>3 assign extension of file to a variable @ext
>>4 assign contents of file to a variable $content
>>5 then insert content with SQL statement where PK
>>6 then do next file until end of directory
>>2) I am getting the die error output when trying to read the file. The
>>code parses the filename fine when i comment out the open file portion

>
>
> you obviously are forgetting the directory part of the
> filename when opening it
>
> gnari
>
>
>

gnari in the code below
I am using $file as the filename to open, if i comment out the open file
and read content portion i am able to parse the the file
is there a reason i can't use $file again



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);
 
Reply With Quote
 
gnari
Guest
Posts: n/a
 
      06-15-2004
"Alexander Heimann" <(E-Mail Removed)> wrote in message
news:r8wzc.6636$US1.3423@fed1read02...
> gnari wrote:
> >
> > you obviously are forgetting the directory part of the
> > filename when opening it
> >

> gnari in the code below
> I am using $file as the filename to open, if i comment out the open file
> and read content portion i am able to parse the the file
> is there a reason i can't use $file again


[snip code where OP is forgetting the directory part]

> opendir (DIR, "D:/D2") or die "couldn't open directory\n";


here 'D:/D2' is the directory, you are reading. call this
the 'directory part'

> print " dir is $dir, name is $name, extension is $ext\n";


here is your problem. your stupid debugging. why print
a bunch of variables that have nothong to do with the problem?
they are not used in the open

> open($input, "<", "$file")
> or die "Couldn't open file :!\n";


always include the filename in the die()
or die "Couldn't open file '$file' :!\n";

if you had done this you would have seen no
directory part ('D:/D2')

gnari




 
Reply With Quote
 
Alexander Heimann
Guest
Posts: n/a
 
      06-15-2004
"gnari" <(E-Mail Removed)> wrote in message news:<camck9$qv6$(E-Mail Removed)>...
> "Alexander Heimann" <(E-Mail Removed)> wrote in message
> news:r8wzc.6636$US1.3423@fed1read02...
> > gnari wrote:
> > >
> > > you obviously are forgetting the directory part of the
> > > filename when opening it
> > >

> > gnari in the code below
> > I am using $file as the filename to open, if i comment out the open file
> > and read content portion i am able to parse the the file
> > is there a reason i can't use $file again

>
> [snip code where OP is forgetting the directory part]
>
> > opendir (DIR, "D:/D2") or die "couldn't open directory\n";

>
> here 'D:/D2' is the directory, you are reading. call this
> the 'directory part'
>
> > print " dir is $dir, name is $name, extension is $ext\n";

>
> here is your problem. your stupid debugging. why print
> a bunch of variables that have nothong to do with the problem?
> they are not used in the open
>
> > open($input, "<", "$file")
> > or die "Couldn't open file :!\n";

>
> always include the filename in the die()
> or die "Couldn't open file '$file' :!\n";
>
> if you had done this you would have seen no
> directory part ('D:/D2')
>
> gnari


Gnar,
I added a variable for the directory part. When i took the die() out
of the open file it worked ok, but when the die was in there it
didn't. For some reason the open file was reading two files with no
filenames in the directory. I don't see the files. i am not really
sure why that is happening. So the open file wouldn't work because
there was no name

Anyways it is working without the die and I think i will be able to
use this now and use the extension and the content of the file
variable in my SQL insert statement. The reason i am printing out
(stupid error checking) is to make sure the variables are holding the
correct values to later use in a SQL statement



use File::Basename;


fileparse_set_fstype("MSDOS");

$mydir = "D:/D2";
opendir (DIR, $mydir) || die "couldn't opendir $mydir: $!\n";
while ($file = readdir(DIR)) {

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

open($input, "<$mydir/$file "); #|| die "couldn't open $mydir/$file
for reading :!\n";
while(<$input>){
undef $/;
$content = <INPUT>;
print if /of/;
print $content;
}
close($input);
}

closedir (DIR);
 
Reply With Quote
 
gnari
Guest
Posts: n/a
 
      06-15-2004
"Alexander Heimann" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...

[using readdir and open]

some of the entries returned by readdir may not be readable files,
for examples directories.
for example '.' and '..'

gnari




 
Reply With Quote
 
Alexander Heimann
Guest
Posts: n/a
 
      06-16-2004
If anyone is interested. I am pasting the code that worked for the
above problem. The main problem i was having was that when I was
reading the directory i forgot to add
next if $file =~/^\.\.?$/; after the while (defined($file =
readdir(DIR))) to skip over the .

after i stopped getting crazy errors

thanks for all your help guys. i will try to contribute as much as I
can i have only been playing with perl for a week now

# modules used
use DBI;
use File::Basename;
use File::Slurp;







#create connection to database
$dbh = DBI->connect('dbi:mysql:$dbname:localhost:3306',
'$username','$password8',
{ RaiseError => 1, AutoCommit => 1});
fileparse_set_fstype("MSDOS");


#open directory loop while there is still a file
$mydir = "D:/desc";
opendir (DIR, $mydir) || die "couldn't opendir $mydir: $!\n";
while (defined($file = readdir(DIR))) {
next if $file =~/^\.\.?$/;


#open file and assign content of file to variable $content
my $content = read_file("$mydir/$file");


#parse file and assing extension to $ext variable
($name, $dir, $ext) = fileparse($file, '\..*');
$ext =~s/^\.//;




#SQL insert statement to insert $ext and $content into DB
#prepare then excecute

$sth= $dbh->prepare("INSERT INTO `desc`VALUES (?,?)");
$sth->execute ( $ext, $content );


}
#close directory
closedir(DIR);
#disconnect from database
$dbh->disconnect();
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      06-18-2004
Alexander Heimann wrote:

> If anyone is interested. I am pasting the code that worked for the
> above problem. The main problem i was having was that when I was
> reading the directory i forgot to add
> next if $file =~/^\.\.?$/; after the while (defined($file =
> readdir(DIR))) to skip over the .


But what if someone creates a subdirectory in D:/D2 ?
The check on /^\.\.$/ is for the cases where files and subdirectories
will both be processed.

In your case, it is more robust to use
next unless -f "$mydir/$file";
to skip anything that is not a plain file (which will skip '.' and '..').

-Joe
 
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