rauschy33 has asked for the wisdom of the Perl Monks concerning the following question:

Hello. I have a problem. I need to write a script to delete files from a directory on NT. I wrote a script that opens the directory and finds the files but I can't UNLINK them. The system says it can't find the files after it printed out the list of files in the directory. Second I need a way to list the date of the files so that I can sort by date from the script and then delete that way. I am new at this perl stuff can anyone help?

Replies are listed 'Best First'.
RE: Deleting Files on NT
by autark (Friar) on Jul 24, 2000 at 18:35 UTC
    I bet you don't supply the full path to the file you are trying to delete.
    my $dir = "dir/foo"; opendir(DIR, $dir); foreach my $file ( readdir DIR ) { unlink "$dir/$file"; }
    As to the dates of files:
    • -M Age of file in days when script started.
    • -A Same for access time.
    • -C Same for inode change time.
    You use it like this:
    my $m_time = -M "$dir/$file";
    But you can also use the stat command. But remember to give a correct path to the file or else it will obviously not work :-)

    As a note, I still do this error all the time :-) Autark

      Ha, your not the only one!
      Thanks for the -M, -A, -C didn't know about them.

RE: Deleting Files on NT
by steveAZ98 (Monk) on Jul 24, 2000 at 18:35 UTC
    Check to make sure the infomation your passing to unlink is correct by putting it in a var first then printing it before you call unlink. This will at least show if you actually do have the correct file and full path. It's hard to tell what the problem is without having the actual error.

    As far as finding the date of the file use stat.
    my $time = (stat($filename))[8];
    I'm not sure what time you'll be sorting by so you may have to use 9 or 10. Also, there is much more info you can get from stat, check out the docs
(jcwren) RE: Deleting Files on NT
by jcwren (Prior) on Jul 24, 2000 at 18:32 UTC
    It would help if you posted your script, but I suspect the problem lies either with relative pathing, or more likely, the forward/backslash issue. Can you post the code fragment (preferrably minimized to the condition that breaks, and don't forget the <code>/</code> tags)?

    --Chris

    e-mail jcwren
Re: Deleting Files on NT
by gaggio (Friar) on Jul 24, 2000 at 18:34 UTC
    Could you please show us your code?

    It would then be much easier to solve your problem, as apparently you have access to the files.

    It is also possible that you would only have a "Read-Only" access, so check it by doing this:

    Win32::File::GetAttributes($file, $attrib);
      Do you really need a module for that? I've always used:
      -w # file is writable -r # file is readable -e # file exists
      Just those three usually cut it when I'm trying to do stuff on the filesystem. If anything more complex than just that is required, I go to stat().

      #!/home/bbq/bin/perl
      # Trust no1!
Re: Help in deleting files in NT
by ColtsFoot (Chaplain) on Jul 24, 2000 at 19:28 UTC
    The following snippet should tell you if your sucessful
    $file = "FileToDelete"; $res = unlink $file; print qq($res\n);
    The number of files deleted will be reported.
    Are you sure you have pernission to write to the file?
    ls -l
    in *nix, but unsure under NT
RE: Help in deleting files in NT
by ferrency (Deacon) on Jul 24, 2000 at 19:37 UTC
    A small code snippet would help us debug your problem. Are you sure you're specifying the full pathname to the file you're unlinking, instead of just the filename? It might be looking for the right filenames in the wrong location.

    Alan