Mega Code Archive

 
Categories / Perl / File
 

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: $!" );