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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Need help
by ELISHEVA (Prior) on Oct 15, 2012 at 10:22 UTC

    First, welcome to the monastery, qwerty80, and to Perl.

    Second, using scripts to delete files automatically when you don't know scripting is dangerous business. If there is a bug in your script you could end up deleting a LOT more than you intended. Please be careful.

    "Delete these file" scripts are not good choices for beginner scripts. If there is any way you can do this manually on the command line I strongly recommend it, even if it will take more time. If you are using a pattern like "abc*.xml", first run a command to list files ( "ls" for unix, "dir" for windows). If you are getting the files you want to delete and only the files you want to delete, then carefully retype the command replacing dir/ls with a command to delete files. If you really must do this via a script, first write your script so that does nothing except print out the list of files, e.g.

    foreach my $file (glob "/tmp/somedir/file*.xml") { print "<$file>\n"; }

    Only after you are really, really sure that you are looping through the files you want to delete and nothing more, only then, should you replace the print "<$file>\n" with code to delete files. If what I've just said sounds too difficult, then again, doing this via script is not safe at all and you should really do this deletion task manually.

    Third, I realize you are feeling desperate, but PerlMonks is not a coding service. We monks do not get real happy about questions like "I need a script give me one", even when one is urgently needed.

    We are here to help people learn how to code in Perl, not to solve their homework or work problems for them. Our payback is the realization that we've helped someone become a slightly better coder and a slightly better coder in Perl particularly. We don't get your salary or your degree, just the satisfaction of helping someone learn.

    For us to get that satisfaction the person has to show us what they want to learn, not simply ask for a solution. I think you will get a much more friendly response from people if you show the code you tried to write and to ask for help rather than say "script please?!".

      Hi ELISHEVA,

      I hope that with "... much more friendly response ..." doesn't mean that my answer was not friendly. I tried to help as fast as requested. And I'm sure that the above code does delete files which match the pattern. I think, I was also polite not using a path of e.g. /etc/sysconfig/*.conf in my example. But I asumed that qwerty80 would take the 15 seconds to look at the code trying to understand as it took me more than 15 seconds to write an answer.

      Best regards

      UPDATE: A ++ for ELISHEVA for being much more polite and friendly as it could be expected by the way it was asked.

Re: Need help
by McA (Priest) on Oct 15, 2012 at 09:41 UTC
    foreach my $file (glob "/tmp/somedir/file*.xml") { unlink $file or die "ERROR: Couldn't delete file '$file': $!"; }
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Need help
by fisher (Priest) on Oct 15, 2012 at 10:09 UTC
    It is fairly easy: first you should opendir the directory, then grep through it's files and unlink the rest.