keltan has asked for the wisdom of the Perl Monks concerning the following question:
HI
I've got problem i need to write file renamer. I know that there are topics on this forum with answers but i need to write slightly different code and i hope you will help. Yes this is my homework but i dont need direct answers i appreciate tips.What i need to write is a script which after this command: ./rename *.pl "s/^/old_/" will add to all files with extension .pl prefix old_ I've been trying few approaches but all failed. Funny thing is that in other languages i don't have that problem only PERL always beat me down.!/usr/bin/perl -w use strict; my $dirname; $dirname = 'D:\test'; my $test = $ARGV[0]; my $test1 = $ARGV[1]; my $pattern = qr/$test/; my $pattern1 = qr/$test1/; opendir(DIR, $dirname) or die "Can't opendir $dirname: $!"; while ( defined (my $file = readdir DIR) ) { next if $file =~ /^\.\.?$/; my $new = $file; $new =~ s/$pattern/$pattern1/; rename($file,$new) }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl file rename
by aaron_baugher (Curate) on Jun 03, 2015 at 00:26 UTC | |
by Anonymous Monk on Jun 03, 2015 at 00:43 UTC | |
|
Re: Perl file rename
by FreeBeerReekingMonk (Deacon) on Jun 02, 2015 at 23:50 UTC | |
by afoken (Chancellor) on Jun 04, 2015 at 07:57 UTC | |
by Anonymous Monk on Jun 04, 2015 at 08:02 UTC | |
|
Re: Perl file rename
by Laurent_R (Canon) on Jun 03, 2015 at 10:13 UTC | |
|
Re: Perl file rename
by Anonymous Monk on Jun 02, 2015 at 23:39 UTC | |
|
Re: Perl file rename
by kcott (Archbishop) on Jun 03, 2015 at 21:20 UTC | |
|
Re: Perl file rename
by afoken (Chancellor) on Jun 04, 2015 at 08:05 UTC |