File open problem

I

IJALAB

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
 
K

Keith Keller

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
 
D

Dan Mercer

:
:
: 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
:
 
J

John W. Krahn

IJALAB said:
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
 
J

Joe Smith

IJALAB said:
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 - $!";
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top