Trouble unlinking files...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • starlight849
    New Member
    • Jun 2009
    • 82

    Trouble unlinking files...

    Hi,
    I am having a little bit of trouble with unlinking a file.

    I would like to unlink it from another directory. The file name contains special characters in it and I have not the ability to change that.

    Here is my code:

    Code:
    sub FileRemove(@)
    {
    my $RemovalFile;
    my @goners = @_;
    
    foreach $RemovalFile(@goners)
     {
     chomp $RemovalFile;
     print "$RemovalFile \n";
     unlink("\"$RemovalFile\"");
    
     my $status = DoesFileExist($RemovalFile);
       
    if ($status)
        {
        `rm "\"$RemovalFile\""`;
         DoesFileExist($RemovalFile);
        }
      }
    }
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    First, get rid of the prototype.

    Using chomp on $RemovalFile is a little odd because that step should have been done with building the array of filenames.

    Why are you surrounding the filename with quotes in the unlink statement? Do your filenames actually begin and end with double quotes? I doubt it and that is the most likely reason the unlink is failing.

    Perl will tell you why it failed to unlink the file, if you had asked it.

    change:
    Code:
    unlink("\"$RemovalFile\"");
    to:
    Code:
    unlink $RemovalFile or die "failed to unlink '$RemoveFile' $!"
    ;

    Why do you have a DoesFileExist() sub when Perl handles that for you with the -e file test operator?
    Code:
    my $status = -e $RemovalFile;

    Comment

    • starlight849
      New Member
      • Jun 2009
      • 82

      #3
      Here is the error. A file or directory in the path name does not exist. at line 176, <$REMOVELIST> line 3
      The file path is right... However the file name contains ± character in it... I was able to get it working doing the rm command but I would prefer to use unlink..

      Could you explain to me why you think I should not use prototypes?

      I followed the suggestions you made. Here is my modified code.

      Code:
      sub FileRemove
      {
      my $RemovalFile;
      my @goners = @_;
      
      foreach $RemovalFile(@goners)
       {
       print "$RemovalFile \n";
       unlink($RemovalFile) or die "failed to unlink $RemovalFile $!";
       my $status = -e $RemovalFile;
      
      if ($status)
          {
          `rm "\"$RemovalFile\""`;
           DoesFileExist($RemovalFile);
          }
        }
      }

      Comment

      • RonB
        Recognized Expert Contributor
        • Jun 2009
        • 589

        #4
        Could you explain to me why you think I should not use prototypes?
        Before reading the info linked below, can you explain how prototypes are used in perl and why you think you should use them here?

        Far More than Everything You've Ever Wanted to Know about Prototypes in Perl

        To handle the special characters, you need to encode/decode the filename.

        Here's a link to a solution of a very similar problem to yours.

        Comment

        • starlight849
          New Member
          • Jun 2009
          • 82

          #5
          When I run the encode, decode on the filename it turns the ± to a ? and still fails to remove the file.

          Comment

          • RonB
            Recognized Expert Contributor
            • Jun 2009
            • 589

            #6
            Please post a short but complete script that demonstrates the problem and a couple sample filenames that fail to be deleted due to the special characters.

            Comment

            • starlight849
              New Member
              • Jun 2009
              • 82

              #7
              ok here is my sample script.
              Here is the output
              Ive been playing around with the different formats of encoding but still haven't got the syntax right.

              Code:
              abc---±.txt
              failed to unlink abc---?.txt A file or directory in the path name does not exist. at test.pl line 23.
              Code:
              #!/usr/local/bin/perl
              
              use strict;
              use warnings;
              use Encode;
              
              my @goners = ('abc---±.txt','def--±.txt');
              my ($status) = ModelRemove(@goners);
              
              sub ModelRemove
              {
              my $RemovalFile;
              my @goners = @_;
              
              foreach $RemovalFile(@goners)
               {
               print "$RemovalFile \n";
               $RemovalFile = encode("gb2312", decode("utf-8", $RemovalFile));
               unlink($RemovalFile) or die "failed to unlink $RemovalFile $!";
                }
              }
              Last edited by starlight849; Mar 11 '13, 08:14 PM. Reason: Update

              Comment

              • RonB
                Recognized Expert Contributor
                • Jun 2009
                • 589

                #8
                If you do the encode/decode, you need to specify the correct encoding.

                I ran a test using your script but without doing the encoding and it worked fine for me.
                Code:
                D:\test\bytes>dir
                 Volume in drive D is DATA
                 Volume Serial Number is B676-6CE5
                
                 Directory of D:\test\bytes
                
                03/11/2013  03:30 PM    <DIR>          .
                03/11/2013  03:30 PM    <DIR>          ..
                03/11/2013  03:29 PM               473 abc---±.txt
                03/11/2013  03:29 PM               659 def--±.txt
                03/11/2013  03:05 PM               392 starlight849.pl
                               3 File(s)          1,524 bytes
                               2 Dir(s)  194,470,682,624 bytes free
                
                D:\test\bytes>starlight849.pl
                abc---▒.txt
                def--▒.txt
                
                D:\test\bytes>dir
                 Volume in drive D is DATA
                 Volume Serial Number is B676-6CE5
                
                 Directory of D:\test\bytes
                
                03/11/2013  03:33 PM    <DIR>          .
                03/11/2013  03:33 PM    <DIR>          ..
                03/11/2013  03:05 PM               392 starlight849.pl
                               1 File(s)            392 bytes
                               2 Dir(s)  194,470,682,624 bytes free

                Comment

                • starlight849
                  New Member
                  • Jun 2009
                  • 82

                  #9
                  You're right it works... It was a problem in the file that was populating my removal list. Thanks so much for all the help Ron, and helping me think. :)

                  Comment

                  Working...