Hey everybody. I've been working on this program and I seem to have hit a bit
of a problem. I'm stumped. Maybe I'm just overlooking something or whatever
but I cannot figure it out. I want it to run at the command line and change
filenames in a specified directory using some search and replace patterns. I want it to support 3 command line flags: -q, -i, --force to help modify the
behavior. I know something's not working right, but I must have missed it, anybody have any advice?
Here's what I have:
$cmdline = shift @ARGV;
foreach (@ARGV)
{
$cmdline = $cmdline . " " . $_;
}
$old = $cmdline;
$cmdline =~ s/\b-i\b//;
if ($cmdline ne $old)
{
$old = $cmdline;
$interactive = 1;
}
else
{
$interactive = 0;
}
$cmdline =~ s/\b--force\b//;
if ($cmdline ne $old)
{
$old = $cmdline;
$force = 1;
}
else
{
$force = 0;
}
$cmdline =~ s/\b-q\b//;
if ($cmdline ne $old)
{
$old = $cmdline;
$quiet = 1;
}
else
{
$quiet = 0;
}
if ( $interactive = 1)
{
$quiet = 0;
}
print "\"$cmdline\"\n";
$search = $ARGV[0]; #search pattern
$replace = $ARGV[1]; #replacement pattern
$dir = $ARGV[2]; #directory
opendir DH, $dir || die "error opening directory, $dir:\n$!\n";
@filez = readdir(DH);
chdir $dir || die "error changing to directory, $dir:\n$!\n";
foreach (@filez)
{
if (-f $_)
{
$old = $_;
s/($search)/$replace/;
if ($quiet != 1)
{
print "$old $_";
}
if ($old ne $_)
{
if ($force == 1)
{
if ($interactive == 1)
{
print " y/n? ";
chomp($y = <STDIN>);
if ($y =~ /^[yY]/)
{
rename $old, $_;
}
}
else
{
rename $old, $_;
print "\n";
}
}
}
else
{
print "\n";
}
}
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.