Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

To delete or not to delete...

Status
Not open for further replies.

bertlacy

Programmer
Nov 13, 2007
1
US
Hey guy's,

I wrote a perl script to list files in a certain directory and then give the user the option of typing in what file he/she wants to encrypt. Everything works fine but I want to give the user the option of Deleting the original file by hitting Y or just hitting N and exiting. I have attached the code that I have thus far... Any ideas?

#!/usr/bin/perl

print "***********************************************************************\n";
print "\tWelcome to the Robert's Encryption Script!\n";
print "***********************************************************************\n\n";


my $gpg = ".gpg";
my $out_dir = "/my/out/";
my $dir = '/my/dir/';

opendir(DIR, $dir) or die "Can't get a file listing in $dir: !";
my @array = readdir DIR;
close DIR;

foreach $file (@array) {
print "\t--- Found this file...\n";
print "\t\t--> $file\n";
}



print "\t***********************************************************\n";
print "\t Please type in what file you would like to encrypt...\n";
print "\t***********************************************************\n\n";

print "File Name... \n";
$temp = <STDIN> ;
chop $temp;
print "You Typed--> $temp \n";

print "\t \n";
print "\t***********************************************************\n";
print "\t Begin encryption of file $temp\n";
print "\t***********************************************************\n\n";



print "\t---Attempting to encrypt $temp\n\n";

system "gpg -e -r rolacy < $dir$temp > $out_dir$temp$gpg";


print "\t~~~ Encrypted file name is $out_dir$temp.gpg\n";
 
What have you tried in regards to deleting? This is very optimistic:

Code:
print "\t---Attempting to encrypt $temp\n\n";
system "gpg -e -r rolacy < $dir$temp > $out_dir$temp$gpg";
print "\t~~~ Encrypted file name is $out_dir$temp.gpg\n";

what happens if the gpg fails to encrypt the script? And allowing user input to be used directly in a sytem() call is asking for trouble.





------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
instead of unlinking the file I'd be tempted to move into a restorable area not unlike a wastebasket. You could store original location, date of 'removal', etc, etc, and when they go into the 'Bin', you could let them restore files. Also, you have a loop, so it may be that you could generate a number, instead of possible typo's on filename entry. Still need to echo the filename for verification.

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
bertlacy
As Kevin has pointed out, its very dangerous to run system command on user input w/o validating it.

For the deletion of the original file part, you can use logic something like:

Code:
print "Do you want to delete the original file... \n";
$file_to_delete = <STDIN> ;
chmop ($file_to_delete)  ;
print "You Typed--> $file_to_delete \n";
print "File to be deleted $dir$temp\n" ;

if ( $file_to_delete =~ /Y/i ) 
{
#	`rm $dir$temp` ;  uncomment this line when you are sure about file to be deleted.
}

P.S. Code NOT tested.

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
use unlink instead of a system call to to

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top