Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > File open problem

Reply
Thread Tools

File open problem

 
 
IJALAB
Guest
Posts: n/a
 
      11-23-2006


I am trying to take the files as command line arguments and do some
formatting to the files based on their extensions. I am getting error
in the file open when i use $ARGV[$i]
Also, I am assigning a fixed name to my files. can i dynamically do
them based on my input file names?

print $#ARGV;
$copypath = "<path>";
for ($i=0;$i<=$#ARGV;$i= $i + 1)
{

$b0 = system("copy $ARGV[$i] $copypath\\scripts");
$b1 = chdir ("$copypath\\scripts");
$extn = substr($ARGV[$i],-4,4);
print $extn, "\n";
print $ARGV[$i];
$file = $ARGV[$i];
if ($extn == ".txt")
{

#Error here!!!
open ($fh, '<$ARGV[$i]') or die "Cannot open Inputfile: ";
open ($ofh, '>OutFile.txt') or die "Cannot open Output File:";


while (my $line = <$fh>) {
<Formatting here>


}


close $fh;
close $ofh;
$b3 = chdir ("$copypath");

}

elsif ($extn == ".csv")
{
#Error here!!!
open my $f2h, '<', '$ARGV[$i]' or die "Cannot open InputFile.txt: ";
open my $of2h, '>', 'OutFile.csv' or die "Cannot open Output File:";


while (my $line = <$f2h>) {
<Formatting exp here>



}


close $f2h;
close $of2h;
$b3 = chdir ("$copypath");
}
}


thanks
bala

 
Reply With Quote
 
 
 
 
Keith Keller
Guest
Posts: n/a
 
      11-23-2006
On 2006-11-23, IJALAB <> wrote:
>
>
> I am trying to take the files as command line arguments and do some
> formatting to the files based on their extensions. I am getting error
> in the file open when i use $ARGV[$i]


See below.

> Also, I am assigning a fixed name to my files. can i dynamically do
> them based on my input file names?


Of course. Parse the input file name for what you want, then
use that parsed name as the name of the output file. Read perlreref
and/or perlretut for help in this area.

> print $#ARGV;


use strict;
use warnings;

> $copypath = "<path>";
> for ($i=0;$i<=$#ARGV;$i= $i + 1)


This is a yucky-looking idiom. Better to use

for my $filename (@ARGV)

Then you can address each file as $filename in your for loop
instead of as $ARGV[$i].

> $b0 = system("copy $ARGV[$i] $copypath\\scripts");


File::Copy is preferable to spawning a subshell.

> $extn = substr($ARGV[$i],-4,4);


Are all your extensions four characters? Mine aren't.

> #Error here!!!
> open ($fh, '<$ARGV[$i]') or die "Cannot open Inputfile: ";


If you read perldoc -f open, you'll notice a lot of references to
the special variable $!, which is the error message that a system
call returns. If you include it, then you will see exactly why
your open is failing.

--keith

--
kkeller-
(try just my userid to email me)
AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt
see X- headers for PGP signature information

 
Reply With Quote
 
 
 
 
Dan Mercer
Guest
Posts: n/a
 
      11-23-2006

"IJALAB" <> wrote in message news: ups.com...
:
:
: I am trying to take the files as command line arguments and do some
: formatting to the files based on their extensions. I am getting error
: in the file open when i use $ARGV[$i]
: Also, I am assigning a fixed name to my files. can i dynamically do
: them based on my input file names?
:
: print $#ARGV;
: $copypath = "<path>";
: for ($i=0;$i<=$#ARGV;$i= $i + 1)
: {
:
: $b0 = system("copy $ARGV[$i] $copypath\\scripts");
: $b1 = chdir ("$copypath\\scripts");
: $extn = substr($ARGV[$i],-4,4);
: print $extn, "\n";
: print $ARGV[$i];
: $file = $ARGV[$i];
: if ($extn == ".txt")
: {
:
: #Error here!!!
: open ($fh, '<$ARGV[$i]') or die "Cannot open Inputfile: ";

no variable substitution inside single quotes

use strict;
use warnings;

Dan Mercer
: open ($ofh, '>OutFile.txt') or die "Cannot open Output File:";
:
:
: while (my $line = <$fh>) {
: <Formatting here>
:
:
: }
:
:
: close $fh;
: close $ofh;
: $b3 = chdir ("$copypath");
:
: }
:
: elsif ($extn == ".csv")
: {
: #Error here!!!
: open my $f2h, '<', '$ARGV[$i]' or die "Cannot open InputFile.txt: ";
: open my $of2h, '>', 'OutFile.csv' or die "Cannot open Output File:";
:
:
: while (my $line = <$f2h>) {
: <Formatting exp here>
:
:
:
: }
:
:
: close $f2h;
: close $of2h;
: $b3 = chdir ("$copypath");
: }
: }
:
:
: thanks
: bala
:


 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      11-23-2006
IJALAB wrote:
>
> I am trying to take the files as command line arguments and do some
> formatting to the files based on their extensions. I am getting error
> in the file open when i use $ARGV[$i]
> Also, I am assigning a fixed name to my files. can i dynamically do
> them based on my input file names?


To add to Dan's and Keith's comments.


> print $#ARGV;
> $copypath = "<path>";
> for ($i=0;$i<=$#ARGV;$i= $i + 1)
> {
>
> $b0 = system("copy $ARGV[$i] $copypath\\scripts");
> $b1 = chdir ("$copypath\\scripts");
> $extn = substr($ARGV[$i],-4,4);
> print $extn, "\n";
> print $ARGV[$i];
> $file = $ARGV[$i];
> if ($extn == ".txt")


You are using a numerical comparison operator so the strings will be converted
to numbers, and unless either string begins with numerical digits they will
both be converted to 0, so that test will almost always be true no matter what
$extn contains. Also Windows file names are case insensitive so you need to
do something like this:

if ( lc( $extn ) eq '.txt' )

Or:

if ( $extn =~ /\.txt\z/i )


> {
>


[ snip ]

> }
>
> elsif ($extn == ".csv")


Same as above.




John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      11-29-2006
IJALAB wrote:
>
> I am trying to take the files as command line arguments and do some
> formatting to the files based on their extensions. I am getting error
> in the file open when i use $ARGV[$i]


Don't use $ARGV[$i]. Use shift() or foreach() instead

> Also, I am assigning a fixed name to my files. can i dynamically do
> them based on my input file names?
>
> print $#ARGV;
> $copypath = "<path>";
> for ($i=0;$i<=$#ARGV;$i= $i + 1)


foreach my $file (@ARGV) {
# use proper indenting
}

> $b0 = system("copy $ARGV[$i] $copypath\\scripts");


my $cmd = "copy $file $copypath\\scripts";
print $cmd,"\n";
(system $cmd) == 0 or warn "Command failed: $?";

> $b1 = chdir ("$copypath\\scripts");


Since you don't undo the chdir() later on, that will fail the second
time if $copypath does not start with "\" or "X:\".

> #Error here!!!
> open ($fh, '<$ARGV[$i]') or die "Cannot open Inputfile: ";


Of course there is an error. '$file' is not the same as "$file" and
you're using single quotes there. Better to use 3-argument open().

open my $fh,'<',$file or die "Cannot open $file - $!";

 
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
Program to open a file in binary, skip X bytes and write the rest ofthe file to a new file scad C++ 4 05-28-2009 08:47 AM
Re: how to open a file in some application using Tkinter i am usingTKINTER to create GUI application i want to know how to open a worddocument in open office or any other applicatio Fredrik Lundh Python 1 01-09-2008 10:40 AM
File::open and File.open Brad Tilley Ruby 4 10-24-2006 09:34 PM
Why cannot open .txt file with Encoding = UNICODE using javascript window.open()? ml Java 0 11-30-2004 07:43 AM
How do I open a database connection on an access file that currently open ? THY ASP .Net 4 08-22-2003 03:50 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57