Deleteing files from Directory

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • imlight211
    New Member
    • Dec 2007
    • 2

    Deleteing files from Directory

    Hi,
    I am also facing a problem while delteing the file. The file name and the path is correct.
    [CODE=perl]if (unlink($entry) == 0) {
    print "File $entry deleted successfully.\n ";
    else {
    print "File $entry was not deleted.\n";
    }[/CODE]and then it prints result as the File <filename> deleted successfully.
    But it actaully doesn't do that. files are still in the directory. Is there any setting i have to do in my script? Could you please elaborate on that. I am new to perl. How do you found out the problem and solution for that.

    Thanks in Advance,
    Deepali
    Last edited by eWish; Dec 18 '07, 01:59 PM. Reason: Added Code Tags
  • rellaboyina
    New Member
    • Jan 2007
    • 55

    #2
    Try this code.

    [CODE=perl]
    use strict;

    my $dir = <path of the directory>;
    opendir DH, $dir or die $!;
    my @files = readdir(DH);
    closedir(DH);
    foreach my $file(@files){
    next if ($file eq "." or $file eq "..");
    my $val = unlink("<path of the directory>\\$fi le");
    if ($val == 0){
    print "\n$file' was deleted successfully";
    }
    else{
    print "\nnot able to delete the file";
    }
    }[/CODE]
    Last edited by eWish; Dec 18 '07, 01:59 PM. Reason: Added Language To Code Tag

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      Originally posted by imlight211
      Hi,
      I am also facing a problem while delteing the file. The file name and the path is correct.
      [CODE=perl]if (unlink($entry) == 0) {
      print "File $entry deleted successfully.\n ";
      else {
      print "File $entry was not deleted.\n";
      }[/CODE]and then it prints result as the File <filename> deleted successfully.
      But it actaully doesn't do that. files are still in the directory. Is there any setting i have to do in my script? Could you please elaborate on that. I am new to perl. How do you found out the problem and solution for that.

      Thanks in Advance,
      Deepali

      Your code is wrong. 0 (zero) indicates failure, not success. 1 (one) indicates success. This is a better way to write the code:

      Code:
      if (unlink $entry) {
      	print "File $entry deleted successfully.\n";
      else {
      	print "File $entry was not deleted: $!\n";
      }
      $! captures any error message from the operating systems alerting you to why the command failed.

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        Originally posted by rellaboyina
        Try this code.

        [CODE=perl]
        use strict;

        my $dir = <path of the directory>;
        opendir DH, $dir or die $!;
        my @files = readdir(DH);
        closedir(DH);
        foreach my $file(@files){
        next if ($file eq "." or $file eq "..");
        my $val = unlink("<path of the directory>\\$fi le");
        if ($val == 0){
        print "\n$file' was deleted successfully";
        }
        else{
        print "\nnot able to delete the file";
        }
        }[/CODE]
        Read my post above as to why your code is also wrong.

        Comment

        • imlight211
          New Member
          • Dec 2007
          • 2

          #5
          Hi, I changed my code accordingly.
          Code:
          bash-2.03$ ls -l
          
          total 68
          
          -rw-r--r--   1 ddeokarb devel         41 Dec 17 02:22 123.csv
          
          -rwxr-xr-x   1 ddeokarb devel         46 Dec 14 06:57 morning
          
          -rw-r--r--   1 ddeokarb devel         45 Nov  6 01:56 myFirstPerl.pl
          
          -rwxr-xr-x   1 ddeokarb devel        611 Dec 14 07:27 scriptQueueDir
          
          drwxr-xr-x   2 ddeokarb devel        512 Dec 14 07:26 temp1
          
          [B]drwxrwxrwx   2 ddeokarb devel        512 Dec 14 07:26 temp2[/B]
          
          drwxr-xr-x   2 ddeokarb devel        512 Dec 14 07:26 temp3
          
          drwxr-xr-x   2 ddeokarb devel        512 Dec 14 07:26 temp4
          
          -rwxr-xr-x   1 ddeokarb devel        663 Dec 14 04:45 try1.pl
          
          -rw-r--r--   1 ddeokarb devel       6518 Dec 14 01:47 updateInProgressSince_DDW.pl
          
          -rw-r--r--   1 ddeokarb devel      10493 Dec 19 07:13 updateInProgressSince_TouchedFiles_DDW.pl
          
          -rw-r--r--   1 ddeokarb devel       6519 Nov  7 05:27 updateProgressDDW.pl
          
          bash-2.03$ cd temp2
          
          [B]bash-2.03$ ls -l
          
          total 0
          
          -rwxrwxrwx   1 ddeokarb devel          0 Dec 14 07:27 9_BLUEBAY__LOADID_682789
          
          -rwxrwxrwx   1 ddeokarb devel          0 Dec 14 07:27 9_BLUEBAY__LOADID_682790
          
          -rwxrwxrwx   1 ddeokarb devel          0 Dec 14 07:27 9_BLUEBAY__LOADID_682791
          
          -rwxrwxrwx   1 ddeokarb devel          0 Dec 14 07:27 9_BLUEBAY__LOADID_682795
          
          -rwxrwxrwx   1 ddeokarb devel          0 Dec 14 07:27 9_BLUEBAY__LOADID_682796
          [/B]
          I have changed the file permission's for directory and files in the directory

          The above is the scrrenshot for the same. I am trying to delete the files from the temp2 directory. But its not deleting…
          Frist I tried the below code
          Code:
          while ( $entry = readdir( DIR ) ) {
                my $last_index=rindex($entry,"_");
                my $tempString;
                if($last_index != -1){
                      $tempString=substr($entry,$last_index+1);
                }
                if($loadIdFromFiles == $tempString)
                {
                     $sendMailFlag=1;
                     my $tempflag=unlink($entry);
                      if ($tempflag == 1) {
                            print "File $entry deleted successfully.\n";
                      } else {
                            print "File $entry was not deleted.\n";
                      }
                }
          }
          then I tried this below code

          Code:
                                                                                                                        foreach my $file(@files){
          next if ($file eq "." or $file eq "..");
          $type = ( -d "$path\\$file" ) ? "dir" : "file"; # $path is crucial!
          my $last_index=rindex($file,"_");
          my $tempString;
          if($last_index != -1){
          $tempString=substr($file,$last_index+1);
          }
          if($loadIdFromFiles == $tempString)
          {
          $sendMailFlag=1;
          if (unlink("$temppath\\$file")){
          print "\n$file' was deleted successfully";
          }else{                                                                                                                                                          print "\nnot able to delete the file $file";                                                                                                        }
          }
          but still I m not able to delete the files…

          any idea?

          Thanks,
          Deepali



          Originally posted by KevinADC
          Your code is wrong. 0 (zero) indicates failure, not success. 1 (one) indicates success. This is a better way to write the code:

          Code:
          if (unlink $entry) {
          	print "File $entry deleted successfully.\n";
          else {
          	print "File $entry was not deleted: $!\n";
          }
          $! captures any error message from the operating systems alerting you to why the command failed.

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            Do this and see what gets printed:

            my $tempflag=unlin k($entry) or die "Can't delete $entry: $!";

            Comment

            • rellaboyina
              New Member
              • Jan 2007
              • 55

              #7
              Thank you kevin for correcting my mistake.

              Deepali, try this code for deleting a file:

              Code:
              use strict;
              
              my $dir = '<path>';
              opendir DH, $dir or die $!;
              my @files = readdir(DH);
              closedir(DH);
              foreach my $file(@files){
                next if ($file eq "." or $file eq "..");
                if(unlink("<path>\\$file")){
                  print "\n$file was deleted successfully";
                }
                else{
                  print "\nnot able to delete the file";
                }
              }

              Comment

              Working...