CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
how to delete file

 



mauto
User

Jul 19, 2002, 9:45 AM

Post #1 of 17 (2217 views)
how to delete file Can't Post

I want to check to see if a file exists, if it does then i want to delete it. How can i do this in perl ?


Pro_4
User

Jul 19, 2002, 3:50 PM

Post #2 of 17 (2211 views)
Re: [mauto] how to delete file [In reply to] Can't Post

this is how i do it...


Code
  

if (-e $file) {

unlink($file);

}



Hope that Helps


mauto
User

Jul 22, 2002, 4:53 AM

Post #3 of 17 (2202 views)
Re: [Pro_4] how to delete file [In reply to] Can't Post

Thanks.

If I've got to delete say 10 files, what would be the best way to achieve this. Can I store them in an array and then delete the lot ?


Jean
User


Jul 22, 2002, 7:30 AM

Post #4 of 17 (2201 views)
Re: [mauto] how to delete file [In reply to] Can't Post

Sure.

You can do unlink @files;

Another option is to do everything inside a loop:
[perl]my $res;
foreach my $file (@files) {
$res = unlink($file);
print "Failure deleting file $file - $!\n" if $res == 0;
}
# OR
foreach my $file (@files) {
unlink($file) if -e $file;
}[/perl]
In the latter case, you can check unlinking of each file while unlinking an array retuns only the number of successfully deleted files...


Jean Spector
SQA Engineer @ Exanet
jean.spector@softhome.net


There are only 10 types of people in the world -
Those who understand binary, and those who don't.


(This post was edited by Jean on Jul 22, 2002, 7:34 AM)


mauto
User

Jul 22, 2002, 10:33 AM

Post #5 of 17 (2195 views)
Re: [Jean] how to delete file [In reply to] Can't Post

Thanks.

I have a long list of files. I want to modify your script to read them from a text file and delete them one at a time if they exist.

currently, I have the following:

my $path = 'c:/temp/';

(@files) = ($path . 'file1.txt', $path . 'file2.txt', $path . 'file3.txt', $path . 'file4.txt', $path . 'file5.txt', $path . 'file6.txt', $path . 'file7.txt', $path . 'file8.txt');

foreach my $file (@files) {
unlink($file) if -e $file;
}


Jean
User


Jul 22, 2002, 11:18 PM

Post #6 of 17 (2190 views)
Re: [mauto] how to delete file [In reply to] Can't Post

The changes you have to make are very slight...

Here is the example:[perl]my $listfile = 'dir/listfile.txt'; # You can omit the 'dir/' part in case the file is located in the same directory as the script
my $res;

open(FILE, $listfile) or die "Error opening file $listfile - $!\n";
while (my $file = <FILE>) { # Read from file line-by-line
chomp $file; # Remove trailing CRLFs
print "Unlinking file $file\n"; # Pring a message to the screen
unlink $file if -e $file; # Delete the file
}
close(FILE);[/perl]


Jean Spector
SQA Engineer @ Exanet
jean.spector@softhome.net


There are only 10 types of people in the world -
Those who understand binary, and those who don't.


(This post was edited by Jean on Jul 22, 2002, 11:22 PM)


Paul
Enthusiast

Jul 23, 2002, 2:43 AM

Post #7 of 17 (2185 views)
Re: [Jean] how to delete file [In reply to] Can't Post

>>
unlink $file if -e $file; # Delete the file
<<

You might want to do something with $! otherwise the files may not unlink and you'll never know.


Jean
User


Jul 23, 2002, 4:04 AM

Post #8 of 17 (2183 views)
Re: [RedRum] how to delete file [In reply to] Can't Post

Mauto:
I agree with [ RedRum ] - it is a really good idea to check whether the file was actually deleted or the function call has failed...

RedRum:
In my first post in this thread I've offered two solutions and one of them used the return value of the unlink() to determine the outcome. Afterwards I've noticed that the thread author chose the solution without error control, thus the last solution Tongue


Jean Spector
SQA Engineer @ Exanet
jean.spector@softhome.net


There are only 10 types of people in the world -
Those who understand binary, and those who don't.


(This post was edited by Jean on Jul 23, 2002, 4:15 AM)


mauto
User

Jul 23, 2002, 6:36 AM

Post #9 of 17 (2178 views)
Re: [Jean] how to delete file [In reply to] Can't Post

Thanks for the advice.

Just one final question. I want to put a header in listfile.txt, which will contain data other than filenames. How can I ensure the script reads from the second line and ignores the header ?


Jean
User


Jul 23, 2002, 6:48 AM

Post #10 of 17 (2176 views)
Re: [mauto] how to delete file [In reply to] Can't Post

Two options:

1. Ignore the first line of the file no matter what - just put the <FILE>; line right after open() call (not very nice, but it does what you need)

2. Ignore ALL lines that begin with the pound sign ('#') - add the next if $file =~ /^\s*#/; as a first line of the while block (much nicer solution) and allows for additional comments. Of course you will have to begin all your comment lines (including the first one) with the pound sign...


Jean Spector
SQA Engineer @ Exanet
jean.spector@softhome.net


There are only 10 types of people in the world -
Those who understand binary, and those who don't.


(This post was edited by Jean on Jul 23, 2002, 6:54 AM)


mauto
User

Jul 23, 2002, 7:00 AM

Post #11 of 17 (2171 views)
Re: [mauto] how to delete file [In reply to] Can't Post

Thanks, option 2 works fine.

Can you explain the line: ~ /^\s*#/;
in words please ?


Jean
User


Jul 23, 2002, 8:26 AM

Post #12 of 17 (2170 views)
Re: [mauto] how to delete file [In reply to] Can't Post

$variable =~ /regex/ means regular expression match (regex is the regular expression itself). The line checks whether the regex matches the contents of the $variable.

Now to the regex:
^ - in the beginning of the regex means the beginning of the line
\s - means whitespace (space, tab etc.)
* - means any number of characters of the previous kind (in our case any number of whitespace - including zero)
# - means #

In other words we match the pound sign in the beginning of the line optionally preceded by any number of whitespace...

If it's not enough, you can look for the regex explanation in the FAQ and there is a regex explanation generator written (if I remember correctly) by [ japhy ]


Jean Spector
SQA Engineer @ Exanet
jean.spector@softhome.net


There are only 10 types of people in the world -
Those who understand binary, and those who don't.


(This post was edited by Jean on Jul 23, 2002, 8:39 AM)


Jean
User


Jul 23, 2002, 8:37 AM

Post #13 of 17 (2168 views)
Re: [mauto] how to delete file [In reply to] Can't Post

I've even found the link to the japhy's (it's his all right) OGRE http://japhy.perlmonk.org/YAPE/OGRE.cgi, but for some reson the site's down Unsure - probably he's moved the OGRE to some other place...


Jean Spector
SQA Engineer @ Exanet
jean.spector@softhome.net


There are only 10 types of people in the world -
Those who understand binary, and those who don't.


Zisu
Novice

Jul 23, 2002, 11:06 PM

Post #14 of 17 (2162 views)
Re: [mauto] how to delete file [In reply to] Can't Post

I found site: http://www.codebits.com/p5be/ to be very useful. Printed almost whole tutorial there, 400 pages or so. Scroll down and look for "chapters" at the left side. Dunno if its the best, but for a beginner like me it'll be enough for a long time.
- Just another beginner -


davorg
Thaumaturge / Moderator

Jul 29, 2002, 2:45 AM

Post #15 of 17 (2152 views)
Re: [mauto] how to delete file [In reply to] Can't Post

Assuming that you have your list of files in @files

Code
$del = unlink grep { -e } @files;

$del will contain the number of files delted successfully.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Paul
Enthusiast

Jul 29, 2002, 7:27 AM

Post #16 of 17 (2148 views)
Re: [davorg] how to delete file [In reply to] Can't Post

>>
$del = unlink grep { -e } @files;
<<

Or perhaps:

$del = unlink grep { -w } @files;


davorg
Thaumaturge / Moderator

Jul 29, 2002, 7:43 AM

Post #17 of 17 (2144 views)
Re: [RedRum] how to delete file [In reply to] Can't Post

On Unix, a file doesn't need to be writable for you to be able to delete it. You just need write permissions on the directory.

OTOH, a file does need to exist before you can delete it :)

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives