Bernhard: Verzeichnisse löschen

Hallo,

ich möchte gerne ein volles Verzeichnis löschen.
Ein leeres ist kein Problem (rmdir)

Versucht hab ich es mit dem einlesen der einzelnen
Dateien und dann mit system und danach (nicht wie hier
zusammen)mit unlink, also so etwa:

$tt="Verzeichnisname";

opendir(VER,$tt);
@datei = readdir(VER);
chdir($tt);
for(@datei)
{
system(del "$_");
unlink("$_");
}

Es tut sich gar nichts, nicht mal ne Fehlermeldung.

Für Eure Hilfe schon mal vielen Dank!

Bernhard

  1. Hi Bernhard,

    vielleicht kannst Du hiermit etwas anfangen.

    Aus dem "Perl Cookbook":

    9.8. Removing a Directory and Its Contents
    Problem
    You want to remove a directory tree recursively without using rm -r.

    Solution
    Use the finddepth function from File::Find, shown in Example 9.3.

    Example 9.3: rmtree1
    #!/usr/bin/perl

    rmtree1 - remove whole directory trees like rm -r

    use File::Find qw(finddepth);
    die "usage: $0 dir ..\n" unless @ARGV;
    *name = *File::Find::name;
    finddepth &zap, @ARGV;
    sub zap {
        if (!-l && -d _) {
            print "rmdir $name\n";
            rmdir($name)  or warn "couldn't rmdir $name: $!";
        } else {
            print "unlink $name";
            unlink($name) or warn "couldn't unlink $name: $!";
        }
    }
    Or use rmtree from File::Path, as shown in Example 9.4.

    Example 9.4: rmtree2
    #!/usr/bin/perl

    rmtree2 - remove whole directory trees like rm -r

    use File::Path;
    die "usage: $0 dir ..\n" unless @ARGV;
        foreach $dir (@ARGV) {
        rmtree($dir);
    }
    WARNING: These programs remove an entire directory tree. Use with extreme caution!

    Discussion
    The File::Find module exports both a find function, which traverses a tree in the (essentially random) order the files occur in the directory, as well as a finddepth function, which is guaranteed to visit all the files underneath a directory before visiting the directory itself. This is exactly what we need to remove a directory and its contents.

    We have to use two different functions, rmdir and unlink. The unlink function deletes only files, and rmdir only deletes empty directories. We need to use finddepth to make sure that we've first removed the directory's contents before we rmdir the directory itself.

    Check first that the file isn't a symbolic link before determining if it's a directory. -d returns true for both a directory and a symbol link to a directory. stat, lstat, and the file test operators like -d all use the operating system call stat (2), which returns all the information kept about a file in an inode. These functions and operators retain that information and let you do more tests on the same file with the special underscore ( _ ) filehandle. This avoids redundant system calls that would return the same information, slowly.

  2. Hallo,

    system(del "$_");
    unlink("$_");

    -versuchs mal ohne die Anfuehrungszeichen, $_ ist ja eine Variable
    -warum system(del, ) und unlink das ist doch doppeltgemoppelt

    SolOng
    Stephan

  3. Hi,

    Es tut sich gar nichts, nicht mal ne Fehlermeldung.

    Es koennte sein, dass du keine Zugriffsrechte auf die Dateien hast,
    das Problem habe ich auch schon gehabt... hat Stunden gedauert, bis
    ich gemerkt habe, woran es liegt *g*

    Alternativ koenntest du aber den "rm -r"-Befehl benutzen:

    open(RM,"rm -r $tt ");

    while(<RM>)
    {
    print $_;
    }

    close(RM);

    mfg
    CK1