command line processing 
Author Message
 command line processing

Hi:

I need to write a program that takes a directory name as an arguement.
To run the
perl program, the command is like
perl program filename1 directoryname filename2
(This program takes three arguments)

I used $#ARGV and $ARGV, but since the perl program needs to run on
Windows system,
which means the directoryname is something like C:\windows\ , (the whole
command line will be like:
perl program filename1 C:\windows\ filename2)
however, back slash is regarded as a special charactor in perl, and thus
the delimiter doesn't work.

Then I used the single quote to quote the directory name, then the
command line will
be:
perl program filename1 'C:\windows\' filename2
but when the directory name is C:\my program file\, (the whole command
like is
perl program filename 'C:\my program file\ ),
which is a legal name on Windows system, the delimiter for the arguments
doesn't work right: the argument number is counted wrong.

Can anybody tell me how to solve this problem?


Thanks!! -- Jun

----------------------
I attached my program here:
-----------------------------------------
if ($#ARGV !=2) {   # $#ARGV equals to the number of arguments minus 1,
   # thus this program requires three arguments.
 print "USAGE: perl dirstrip.pl oldfilename 'directoryname'
newfilename\n";
 die "Error: argument number wrong\n";

Quote:
}

#because the directory name contains special characters "\", we need ask
user
#to quote it to make it accepted by the program without screwing up the
program.

$oldfilename = $ARGV[0];   # the first argument is the old file name
$pathname =$ARGV[1];    # the second arguement is the directory name
that the user wants to
   # deliminate.
$pathname =~ s/^'//;    # deliminate the ' at the beginning of the
pathname
$pathname =~ s/'$//;    # deliminate the ' at the end of the pathname
$newfilename =$ARGV[2]; # the third argument is the new file name

#following three lines are used for debugging
#print $oldfilename."\n";
#print $pathname."\n";
#print $newfilename."\n";

#OLDFILE is the file handle
#$oldfilename is the filename
open (OLDFILE, $oldfilename) || die "Sorry, cannot
open".$oldfilename."\n" ;

#open this file for #writing
open(NEWFILE, ">". $newfilename) || die "Sorry, cannot open
".$newfilename." to write\n" ;

while (<OLDFILE>) {

 $newline = $_ ;
 #$newline =~ s/$pathname//;
 $newline =~ s/^\Q$pathname\E//;  #\Q ... \E hold the #path name (path
uses back slash, which can't be used #this way in perl, so you use
holders
 print NEWFILE $newline; # this line is taking data from previous line
and recording the newline data into the newfile using the handle the
NEWFILE.

Quote:
} #all data inside the brackets {}is a "while" loop - #while the

condition is true repeat loop until false

close (OLDFILE);
close (NEWFILE);



Fri, 06 Jul 2001 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. HELP: Command line processing

2. Perl command line processing, Windows/dos style ?

3. HELP: Command line processing

4. basic query about command line processing in Perl

5. Simple I/O to a command-line process?

6. ANNOUNCE: Getopt::Long 2.9 -- Universal command line options processing

7. patch1 for wafeperl 0.2 to allow xt command line argument processing

8. ANNOUNCE: Getopt::Long 2.6 -- Universal command line options processing

9. Processing command line.

10. Processing command line

11. ANNOUNCE: Getopt::Long 2.9 -- Universal command line options processing

12. ANNOUNCE: Getopt::Long 2.6 -- Universal command line options processing

 

 
Powered by phpBB® Forum Software