#  rename1.pl
#  like rename1.pl but saves existing firstitem ( e.g. "k-" ) at the begining
#  replaces one item in filename to another, to customise !!!
#  write STDOUT to file e.g. r.bat ( not ren.bat ! does not work ! )
#
#  writes DOS-Batch to rename file's oldname to newname
#
# todo: param for searchitem/replaceitem, usage
#
#  060329, 060527

#my $search_item = "_David-" ;
# my $search_item = "mvi_" ;
 my $search_item = "img_" ;
# my $replace_item = "_26-" ; 
#my $replace_item = "Weihnachten_David-2008_12_26-" ; 
my $replace_item = "Achenkirch-2009_02_05-";
my ( $oldname, $partname, $newname, $dirfile, $firstitem  ) ;

$dirfile = "dir.txt" ;
system( "dir /b > $dirfile" ) ;
open( DF, "<$dirfile") or die "no $dirfile\n" ;
while(<DF>) {
   chomp;
   $oldname = $_ ;
   if ( $oldname =~ /(.*)$search_item(.*)/i )  { 
      $firstitem = $1 ;
#      if ( !defined $1 && !defined $2 )  { next; } 
      if ( !defined $2 )  { next; } 
      s/$search_item/$replace_item/i ;
      $newname = $_ ;
#      print "newname: $newname\n" ;
#      print "ren \"$oldname\" \"$firstitem$newname\"\n"; 
      print "ren \"$oldname\" \"$newname\"\n"; 
   }
}
close $dirfile ;
unlink $dirfile ; # does not delete $dirfile !
print "if exist $dirfile del $dirfile\nif exist aaa.bat del aaa.bat"

__END__


