Remove files in directories

I am trying to remove files from a couple of directories after a process completes. Below is what I have so far, but the command does not run and cygwin closes too fast to read the error. Also, is it possible to have the printf be displayed until the files are removed then printf in italics is displayed and the program exits? Thank you :).

 
remove() {
    printf "\n\n"
    printf "Removing old files, please wait ";
  # annovar directory
  my $filename = qw[ 'C:\Users\cmccabe\Desktop\annovar\out_position.txt', 'C:\Users\cmccabe\Desktop\annovar\out_parse.txt' ];
  unlink $filename or die "Couldn't unlink($filename) : ($!)($^E)";
  
  # python directory
  my $filename = qw[ 'C:/Users/cmccabe/Desktop/Python27/${id}.txt', 'C:/Users/cmccabe/Desktop/Python27/out_name.txt' ];
  unlink $filename or die "Couldn't unlink($filename) : ($!)($^E)";
  printf "\n Old files removed, Goodbye! \n\n"; sleep 2 && exit
}
  1. How are you invoking this perl sub-routine? If this is in a perl script, then are you invoking it on cygwin command line?

  2. $id is not defined anywhere in the scope of remove() routine.

  3. Just for the sake of debugging, you could have some mechanism to wait until the user enters an input and quit after that.

I am invoking the sub-routine as part of a bash menu. Basically, the user is prompted with a Yes/No selection and a No response leads to the sub-routine: Is there a better way to remove files after a script completes?

I modified it a bit and it runs now but the files still remain in the directory and are not removed. $id.txt is a variable in the python directory that represents the id that the user types in. Thank you :).

remove() {
    printf "\n\n"
    printf "Removing old files, please wait ";
	
  # annovar directory
  my $filename = qw[ 'C:\Users\cmccabe\Desktop\annovar\out_position.txt' ];
  my $filename = qw[ 'C:\Users\cmccabe\Desktop\annovar\out_parse.txt' ];
  unlink $filename or die "Couldn't unlink($filename) : ($!)($^E)";
  
  # python directory
  cd cd 'C:'
  my $filename = qw[ C:/Users/cmccabe/Desktop/Python27/$id.txt ];
  my $filename = qw [ C:/Users/cmccabe/Desktop/Python27/out_name.txt ];
  unlink $filename or die "Couldn't unlink($filename) : ($!)($^E)";
  
  printf "\n Old files removed, Goodbye! \n\n"; sleep 2 && exit
} 

Remembering well your other threads in which you built shell scripts with only a few python commands, I'd second balajesuri"s assumption that you mix shell/perl/python commands. Why don't you just use the rm command in shell?

Something like:

 
# delete old samples
rm 'C:\Users\cmccabe\Desktop\annovar\out_position.txt'
rm 'C:\Users\cmccabe\Desktop\annovar\out_parse.txt' 

Thank you :).

Yes. And check the exit codes after each rm to output an error msg if need be.

 # delete old samples
rm 'C:\Users\cmccabe\Desktop\annovar\out_position.txt' || :
rm 'C:\Users\cmccabe\Desktop\annovar\out_parse.txt' || : 

This will make the delete run if there are errors, right? Is there a way to return what files couldn't be removed? Thank you :).

Don't run : (it's a NoOp) but e.g. echo the filename in an error message.

1 Like

Thank you :).