Renaming a file before accidental deletion : rename « File « Perl






Renaming a file before accidental deletion

   

#!/usr/bin/perl


use warnings;
use strict;

if ( -e 'file.txt' ) {
   print( "Do you want to write over file.txt? (yes or no): " );
   chomp( my $response = <STDIN> );
   rename( 'file.txt', 'file.old' ) or die( "Error renaming : $!" ) if ( $response eq 'no' );
}

open( FILE, ">file.txt" ) or die( "Error opening: $!" );
print( FILE "A copy of file.txt is saved in file.old.\n" );
close( FILE ) or die( "Cannot close: $!" );

   
    
    
  








Related examples in the same category

1.Rename a file
2.Rename a group of files with a common extension?
3.Renaming Files: rename(OLDFILENAME, NEWFILENAME);