Deleting files

A

Anders F

Is there a Perl subroutine for deleting files?

My problem is that I have a perl script to backup files.
First it copies all files I want to backup to a temporary directory,
then it zips the temporary directory and puts the result in a
permanent backup directory which I can copy to some external
device.

After zipping, I want to delete the temporary directory and I can
do this with system( "del ..."); and system( "rmdir ...");
My problem is that del prints the name of every file it deletes
filling the whole Command Promt buffer. Besides being irritating,
this means that if something goes wrong before I delete the files,
I can not see it.

So I want some alternate way to delete files. If possible deleting
a directory with all files and subdirectories in one command,
else it should not bee too difficult to make a recursive subroutine
and delete the files one by one.

(I have Windows XP SP2. The "del" command has no switch to supress
printout.)

Thanks for any answer
Anders
 
B

Bart Lateur

Anders said:
Is there a Perl subroutine for deleting files?

There's a builtin. It's called "unlink".

http://perldoc.perl.org/functions/unlink.html

The reason for the odd name is because on Unix, there's no garantee that
if you succesfully apply this function on a file, that it will indeed be
deleted. There' something called a "hard link", so there can be multiple
directory entries, possibly in more than one directory, for the same
file. "unlink" will remove one such directory entry (called a "link").

Only when all links are gone, will the file contents indeed be deleted.
 
P

Paul Lalli

Is there a Perl subroutine for deleting files?

Sherm already gave you a fish, so I'll teach you how to fish instead.

`perldoc perlfunc` has a list of all Perl functions, grouped by
category. You can either open a command prompt and type
perldoc perlfunc
or you can go to your Start Menu, All Program, ActivePerl,
Documentation. And from there, click on 'perlfunc' on the left
frame.

Within that list of all Perl functions grouped by category, you'll
find:
Functions for filehandles, files, or directories
"-X", "chdir", "chmod", "chown", "chroot", "fcntl",
"glob", "ioctl", "link", "lstat", "mkdir", "open",
"opendir", "readlink", "rename", "rmdir", "stat",
"symlink", "sysopen", "umask", "unlink", "utime"


Hope this helps,
Paul Lalli
 
T

Ted Zlatanov

AF> Is there a Perl subroutine for deleting files?

Sure, do `perldoc -f unlink'

AF> So I want some alternate way to delete files. If possible deleting
AF> a directory with all files and subdirectories in one command,

You probably want to use File::path:

http://search.cpan.org/~dland/File-Path-1.99_02/Path.pm

AF> else it should not bee too difficult to make a recursive subroutine
AF> and delete the files one by one.

Sure, you can do that. It's very educational if you want to learn more
about files and directories.

Ted
 
T

Tim S

Bart said:
There's a builtin. It's called "unlink".

http://perldoc.perl.org/functions/unlink.html

The reason for the odd name is because on Unix, there's no garantee that
if you succesfully apply this function on a file, that it will indeed be
deleted. There' something called a "hard link", so there can be multiple
directory entries, possibly in more than one directory, for the same
file. "unlink" will remove one such directory entry (called a "link").

Only when all links are gone, will the file contents indeed be deleted.

And indeed, when the final handle to the file is closed:

eg:

open F, ">Somefile";
unlink "Somefile";

print F "Wibble\n"; # File data exists, but there is no way to re-open
# it as the directory entry has gone. However, F may be cloned or passed to
# a sub process via fork().

close F; # Now the file goes away

Cheers

Tim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top