Perl Programming Language
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
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
Thanks for any answer
perldoc -f unlink
"Anders F" <nos
> Is there a Perl subroutine for deleting files?
perldoc -f rmdir
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net
Anders F wrote: There's a builtin. It's called "unlink".
>Is there a Perl subroutine for deleting files?
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.
On Jun 2, 1:46 pm, "Anders F" <firstname.lastname@example.org> wrote:
> 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
or you can go to your Start Menu, All Program, ActivePerl,
Documentation. And from there, click on 'perlfunc' on the left
Within that list of all Perl functions grouped by category, you'll
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,
On Sat, 02 Jun 2007 17:46:11 GMT "Anders F" <email@example.com> wrote:
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:
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.
And indeed, when the final handle to the file is closed:
open F, ">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