473,387 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Deleteing files from Directory

Hi,
I am also facing a problem while delteing the file. The file name and the path is correct.
Expand|Select|Wrap|Line Numbers
  1. if (unlink($entry) == 0) {
  2.     print "File $entry deleted successfully.\n";
  3. else {
  4.     print "File $entry was not deleted.\n";
  5. }
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
Dec 18 '07 #1
6 1335
Try this code.

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2.  
  3. my $dir = <path of the directory>;
  4. opendir DH, $dir or die $!;
  5. my @files = readdir(DH);
  6. closedir(DH);
  7. foreach my $file(@files){
  8.   next if ($file eq "." or $file eq "..");
  9.   my $val = unlink("<path of the directory>\\$file");
  10.     if ($val == 0){
  11.       print "\n$file' was deleted successfully";
  12.     }
  13.     else{
  14.       print "\nnot able to delete the file";
  15.     }
  16. }
Dec 18 '07 #2
KevinADC
4,059 Expert 2GB
Hi,
I am also facing a problem while delteing the file. The file name and the path is correct.
Expand|Select|Wrap|Line Numbers
  1. if (unlink($entry) == 0) {
  2.     print "File $entry deleted successfully.\n";
  3. else {
  4.     print "File $entry was not deleted.\n";
  5. }
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:

Expand|Select|Wrap|Line Numbers
  1. if (unlink $entry) {
  2.     print "File $entry deleted successfully.\n";
  3. else {
  4.     print "File $entry was not deleted: $!\n";
  5. }
  6.  
$! captures any error message from the operating systems alerting you to why the command failed.
Dec 18 '07 #3
KevinADC
4,059 Expert 2GB
Try this code.

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2.  
  3. my $dir = <path of the directory>;
  4. opendir DH, $dir or die $!;
  5. my @files = readdir(DH);
  6. closedir(DH);
  7. foreach my $file(@files){
  8.   next if ($file eq "." or $file eq "..");
  9.   my $val = unlink("<path of the directory>\\$file");
  10.     if ($val == 0){
  11.       print "\n$file' was deleted successfully";
  12.     }
  13.     else{
  14.       print "\nnot able to delete the file";
  15.     }
  16. }
Read my post above as to why your code is also wrong.
Dec 18 '07 #4
Hi, I changed my code accordingly.
Expand|Select|Wrap|Line Numbers
  1. bash-2.03$ ls -l
  2.  
  3. total 68
  4.  
  5. -rw-r--r--   1 ddeokarb devel         41 Dec 17 02:22 123.csv
  6.  
  7. -rwxr-xr-x   1 ddeokarb devel         46 Dec 14 06:57 morning
  8.  
  9. -rw-r--r--   1 ddeokarb devel         45 Nov  6 01:56 myFirstPerl.pl
  10.  
  11. -rwxr-xr-x   1 ddeokarb devel        611 Dec 14 07:27 scriptQueueDir
  12.  
  13. drwxr-xr-x   2 ddeokarb devel        512 Dec 14 07:26 temp1
  14.  
  15. drwxrwxrwx   2 ddeokarb devel        512 Dec 14 07:26 temp2
  16.  
  17. drwxr-xr-x   2 ddeokarb devel        512 Dec 14 07:26 temp3
  18.  
  19. drwxr-xr-x   2 ddeokarb devel        512 Dec 14 07:26 temp4
  20.  
  21. -rwxr-xr-x   1 ddeokarb devel        663 Dec 14 04:45 try1.pl
  22.  
  23. -rw-r--r--   1 ddeokarb devel       6518 Dec 14 01:47 updateInProgressSince_DDW.pl
  24.  
  25. -rw-r--r--   1 ddeokarb devel      10493 Dec 19 07:13 updateInProgressSince_TouchedFiles_DDW.pl
  26.  
  27. -rw-r--r--   1 ddeokarb devel       6519 Nov  7 05:27 updateProgressDDW.pl
  28.  
  29. bash-2.03$ cd temp2
  30.  
  31. bash-2.03$ ls -l
  32.  
  33. total 0
  34.  
  35. -rwxrwxrwx   1 ddeokarb devel          0 Dec 14 07:27 9_BLUEBAY__LOADID_682789
  36.  
  37. -rwxrwxrwx   1 ddeokarb devel          0 Dec 14 07:27 9_BLUEBAY__LOADID_682790
  38.  
  39. -rwxrwxrwx   1 ddeokarb devel          0 Dec 14 07:27 9_BLUEBAY__LOADID_682791
  40.  
  41. -rwxrwxrwx   1 ddeokarb devel          0 Dec 14 07:27 9_BLUEBAY__LOADID_682795
  42.  
  43. -rwxrwxrwx   1 ddeokarb devel          0 Dec 14 07:27 9_BLUEBAY__LOADID_682796
  44.  
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
Expand|Select|Wrap|Line Numbers
  1. while ( $entry = readdir( DIR ) ) {
  2.       my $last_index=rindex($entry,"_");
  3.       my $tempString;
  4.       if($last_index != -1){
  5.             $tempString=substr($entry,$last_index+1);
  6.       }
  7.       if($loadIdFromFiles == $tempString)
  8.       {
  9.            $sendMailFlag=1;
  10.            my $tempflag=unlink($entry);
  11.             if ($tempflag == 1) {
  12.                   print "File $entry deleted successfully.\n";
  13.             } else {
  14.                   print "File $entry was not deleted.\n";
  15.             }
  16.       }
  17. }                                                     
  18.  
then I tried this below code

Expand|Select|Wrap|Line Numbers
  1.                                                                                                               foreach my $file(@files){
  2. next if ($file eq "." or $file eq "..");
  3. $type = ( -d "$path\\$file" ) ? "dir" : "file"; # $path is crucial!
  4. my $last_index=rindex($file,"_");
  5. my $tempString;
  6. if($last_index != -1){
  7. $tempString=substr($file,$last_index+1);
  8. }
  9. if($loadIdFromFiles == $tempString)
  10. {
  11. $sendMailFlag=1;
  12. if (unlink("$temppath\\$file")){
  13. print "\n$file' was deleted successfully";
  14. }else{                                                                                                                                                          print "\nnot able to delete the file $file";                                                                                                        }
  15. }
but still I m not able to delete the files…

any idea?

Thanks,
Deepali



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

Expand|Select|Wrap|Line Numbers
  1. if (unlink $entry) {
  2.     print "File $entry deleted successfully.\n";
  3. else {
  4.     print "File $entry was not deleted: $!\n";
  5. }
  6.  
$! captures any error message from the operating systems alerting you to why the command failed.
Dec 19 '07 #5
KevinADC
4,059 Expert 2GB
Do this and see what gets printed:

my $tempflag=unlink($entry) or die "Can't delete $entry: $!";
Dec 19 '07 #6
Thank you kevin for correcting my mistake.

Deepali, try this code for deleting a file:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2.  
  3. my $dir = '<path>';
  4. opendir DH, $dir or die $!;
  5. my @files = readdir(DH);
  6. closedir(DH);
  7. foreach my $file(@files){
  8.   next if ($file eq "." or $file eq "..");
  9.   if(unlink("<path>\\$file")){
  10.     print "\n$file was deleted successfully";
  11.   }
  12.   else{
  13.     print "\nnot able to delete the file";
  14.   }
  15. }
Dec 20 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip...
3
by: Emilio | last post by:
Hi, How can I find out what process are using a particular file, I want my users to be able to delete obsolete images, html pages etc. But I always get an error mentioning that the file file is...
6
by: Peter Row | last post by:
Hi, Can someone give me a set of steps for an ASP.NET project (well actually its just a VB.NET class DLL that implements HttpHandler) that will work when moved to another developers machine? ...
5
by: Bas Hendriks | last post by:
Has anyone any idea how asp.net find it's files back after compiling them to the temporary asp.net directory? I found on numerous webpages that the directorynames are chosen random but cannot find...
18
by: UJ | last post by:
Folks, We provide custom content for our customers. Currently we put the files on our server and people have a program we provide that will download the files. These files are usually SWF, HTML or...
10
by: msnnews.msn.com | last post by:
hi there, i've got a photo gallery that programatically creates a subfolder for each gallery, if i delete the album in my site, it also deletes the subfolder, but resets my website and i lose...
2
by: express99 | last post by:
i am new to perl scripting. i want to be able to delete folders and files older than n days recursively in a windows environment.
6
by: Smokey Grindle | last post by:
I need to ensure that there are NO files in the target folder of a setup project, I want to delete anything in the folder first before the install takes place (all from inside the installer program...
4
Chrisjc
by: Chrisjc | last post by:
Good evening, I was wondering if anyone knew of a way or something to help me out with an issue I been dealing with lately, I have several sites in an Active Directory environment across a WAN....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.