|
|
 |
 |
 |
 |
Perl Programming Language
|
 |
 |
 |
 |
 |
 |
 |
 |
Deleting files
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
"Anders F" <nos @example.com> writes: > Is there a Perl subroutine for deleting files? perldoc -f unlink perldoc -f rmdir sherm-- -- Web Hosting by West Virginians, for West Virginians: http://wv-www.net Cocoa programming in Perl: http://camelbones.sourceforge.net
Anders F wrote: >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. -- Bart.
On Jun 2, 1:46 pm, "Anders F" <nos@example.com> 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 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
On Sat, 02 Jun 2007 17:46:11 GMT "Anders F" <nos@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: 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
Bart Lateur wrote: > Anders F wrote: >>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.
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
|
 |
 |
 |
 |
|