Current location: Hot Scripts Forums » Programming Languages » Perl » Project Help


Project Help

Reply
  #1 (permalink)  
Old 01-14-04, 07:51 AM
azonic azonic is offline
New Member
 
Join Date: Jan 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Project Help

I'm taking an introductory course into computer programming and I needed to write a Perl program for renaming files in a specified directory according to regular expression search and replace patterns given on the command line.

Has to support 4 optional command line flags -f, -u, -d, and -q to modify it's behavior. I made an error somewhere along the way, and I need someone to help me since I'm not a programmer at all

Here's the code, if anyone could fix it or write it in a different way of performing the task I'd greatly appreciate it. If not that's ok . Here's what I wrote...

Code:
@args = ();
$forceFlag = 0;   # -f
$upcaseFlag = 0;  # -u
$lowcaseFlag = 0; # -d
$quietFlag = 0;   # -q

$error = 0;
# iterate through the arguments
foreach $arg ( @ARGV )
{
	$first = substr($arg, 0, 1);
	if ($first eq "-")
	{
		# we've found one or more switches
		foreach $letter ( split('',$arg) )
		{
			if ( $letter eq '-' )
			{ 
				# do nothing 
			}
			elsif ( $letter eq 'f' )
			{
				$forceFlag = 1;
			}
			elsif ( $letter eq 'u' )
			{
				$lowcaseFlag = 0;
				$upcaseFlag = 1;
			}
			elsif ( $letter eq 'd' )
			{
				$upcaseFlag = 0;
				$lowcaseFlag = 1;
			}
			elsif ( $letter eq 'q' )
			{
				$quietFlag = 1;
			}
			else
			{
				print "Invalid switch -$letter\n";
				$error = 1;
			}
		}
	}
	else
	{
		# we've found an argument
		push(@args, $arg)
	}
}
if ( $error )
{
	exit(1);
}

$numArgs = $#args + 1;

if ( $numArgs < 1 )
{
	print "Error: no directory specified.";
	exit(1);
}
else
{
	$searchDir = $args[0];
}

if ( $numArgs < 2 )
{
	print "Error: no search pattern specified.\n";
	exit(1);
}
else
{
	$searchPattern = $args[1];
}

if ( $numArgs == 3 )
{
	$replacePattern = $args[2];
}

$totalfiles = 0;
$validfiles = 0;

opendir(D, $searchDir);
@files = readdir(D);
closedir(D);
foreach $file (@files)
{
	my $filename = $file;
	my $originalName = $filename;
    if ($file eq '.' || $file eq '..' || -d $file) 
    {    } 
    else 
    {
		 if ( $filename =~ /$searchPattern/ )
		 {
			 $totalfiles++;
			 if ( $upcaseFlag )
			 {
				 $filename = uc($filename);
			 }
			 if ( $lowcaseFlag )
			 {
				 $filename = lc($filename);
			 }
		 }
		 my $resultName;
		 if ( $replacePattern )
		 {
		 	$resultName = renameFile($searchPattern, $replacePattern, $filename);	   
	 	 }
	 	 else
	 	 {
		 	 $resultName = $filename;
	 	 }
		 if ( $resultName ne $originalName )
		 {
			 $validfiles++;
			 if ( $forceFlag )
			 {
				 rename($searchDir . '/' . $originalName, $searchDir . '/' . $resultName);
			 }
	 		 if ( !$quietFlag )
			 {
			 	 print $searchDir . '/' . $originalName . " -> " . $searchDir . '/' . $resultName . "\n";
		 	 }	
		 }
	 }
}

if ( $forceFlag )
{
	print "$validfiles file(s) were renamed out of $totalfiles file(s) that matched the search pattern.\n";
}
else
{
	print "$validfiles file(s) could be renamed out of $totalfiles file(s) that matched the search pattern.\n";
}


sub renameFile
{
	my $searchPattern = shift;
	my $replacePattern = shift;
	my $filename = shift;
	$filename =~ s/$searchPattern/$replacePattern/g;
	return $filename;
}
Reply With Quote
  #2 (permalink)  
Old 01-14-04, 04:40 PM
nakulgoyal nakulgoyal is offline
Coding Addict
 
Join Date: Jun 2003
Posts: 283
Thanks: 0
Thanked 0 Times in 0 Posts
Re:

Welcome to PT !!
Reply With Quote
  #3 (permalink)  
Old 01-15-04, 09:26 AM
Chas Chas is offline
Coding Addict
 
Join Date: Oct 2003
Location: California
Posts: 359
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by azonic
I made an error somewhere along the way, and I need someone to help me since I'm not a programmer at all
What's wrong with it? Details on the problem(s) will give me an idea of where to start looking.

~Charlie

p.s. Very nice code for not being a programmer!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
can they SUE? based on same features, ideas, concept? soccer The Lounge 6 01-20-09 05:28 AM
Help Wanted for project smeech ASP 2 01-09-04 01:25 PM
Web Project Design Problem Help me out coolvcguy ASP 0 10-16-03 11:46 AM
Software development from project web tools to sowftware component website scripts SoftwareObjects Job Offers & Assistance 0 06-17-03 08:38 PM


All times are GMT -5. The time now is 04:51 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.